Here is a code that retrieve all the tags (under a tag or all in the page).
Can be usefull when you try to reach all elements under a certain div.
Source / Exemple :
/*
getAllElements.js
Author: Benjamin
Version: 1.1
Creation: Unknow
LastModification: 22.02.2011
Copyright: Free of use if you mention the author and source
Version 1.0
=> Retrieve all tags element in the page.
/*
function getNodes(beginNode,bool)
Return: nothing
Atrributes:
beginNode: the tag name to start from;
Method:
get the children of the current tag;
loop the childs
when child is a tag do some work
if the child as children recall the function with the tag for beginNode
function getNodes(beginNode){
var nodes = beginNode;
nodes = beginNode ? beginNode.childNodes : document.childNodes;
for(var i=0; i < nodes.length ; i++){
if(nodes[i].nodeType==1){
/*
Do something here
In the exemple give the name of the tag in alert mode
alert(nodes[i].nodeName);
if(nodes[i].childNodes){
getNodes(nodes[i]);
}
}
}
}
//get all elements in the page includes head tags
getNodes();
//get only the element under the specified tag
var nodes = document.getElementsByTagName('div')[0]; //here the first div childs
getNodes(nodes);
Conclusion :
I hope this code will help some people like he helped me.
14 mars 2011 à 17:51
C'est plus un snippet qu'un code a mon avis...
Je crois qu'il y a un endroit pour ca dans codes-sources.
Le niveau initie me parait un peu usurpe aussi...
Ca peut etre educatif a la rigueur mais l'interet est un peu limite pour des applications reelles car tous les frameworks modernes (prototypejs, jQuery, etc...) proposent ce genre de choses en standard (et en beaucoup plus flexible et performant (Regarde les implementations JS des selecteurs CSS3 si tu veux savoir comment ca marche)).
Eric
23 févr. 2011 à 09:27
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.