I have this DOM tree:
<li>
data ....
<br>
data ...
<img ... />
<br>
<script> ... </script>
<span> data ... </span>
</li>
how can I loop through the children of the above li element that fall between the li itself and script element, ie script and span elements are out of the loop ... thanks in advace !!
note: I do not want to use JQuery, because my app is using Protoype and it will conflict with JQuery if I use both of them together, i'd appreciate it if there's a quick solution using Prototype.
would something like this work for you?
var child = liElement.firstChild;
while(child){
if(child.nodeName.toLowerCase() == 'script'){
break
}
//do your stuff here
child = child.nextSibling;
}
Note, the if the "data" in your example are strings, these will exist in the child heirarchy as instances of textNodes. if you need to do special processing on this stuff, you will want to do a check such as
switch(child.nodeType){
case 3:
//text Node
break;
case 1:
//Element node
break;
default:
//break;
}
check out https://developer.mozilla.org/en/nodeType for more node types
You could use jQuery selectors:
Get all children of <li>
:
$('li').children();
Exclude unwanted elements:
$('li').children().not('script').not('span');
Then loop over the selection:
$('li').children().not('script').not('span').each(function(index, element){
//do sth like hide the element
$(element).hide();
});
here's a solution i hacked for you :
$( "li" ).each(function( index ) {
console.log($(this).find("span:first").text());
});
hav eyou taken a look at jquery ? it has a very nic query syntax. i`m nit sure what elements exactly you want to loop over but for intance: $('li > img') will return all images under all list items. check out the spec for more jquery
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.