Home > Web Front-end > JS Tutorial > JS uses for loop to traverse child nodes to find elements_javascript tips

JS uses for loop to traverse child nodes to find elements_javascript tips

WBOY
Release: 2016-05-16 16:36:58
Original
1880 people have browsed it

This article mainly introduces JS using for loop and array to traverse child nodes to find elements

function nextChildNode(node,clazz,tagName){ 
var count= node.childElementCount; 
for(var i=0;i<count;i++){ 
if(node==undefined || node.children[i]==undefined){ 
continue; 
} 
if(clazz){ 
if(node.children[i].getAttribute('class')==clazz){ 
return node.children[i]; 
} 
}else{ 
if(node.children[i].tagName==tagName){ 
return node.children[i]; 
} 
} 


} 
return null; 
} 

function getChildNode(node,classArg,tagNodeArg){ 
for(var i=0;i<classArg.length;i++){ 
node=nextChildNode(node,classArg[i]); 
} 
for(var i=0;i<tagNodeArg.length;i++){ 
node=nextChildNode(node,null,tagNodeArg[i]); 
} 
return node; 
} 

function getItemId(node){ 
var classNode=['itemInfo','itemDesc'],tagNode=['P','BUTTON']; 
node=getChildNode(node,classNode,tagNode); 
alert(node.getAttribute('itemid')); 

} 

#调用函数 
getItemId($(".shopItem")[0]); 
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template