JS 使用for循环遍历子节点查找元素
时间:2020-12-08 22:49:15|栏目:JavaScript代码|点击: 次
这篇文章主要介绍了JS 使用for循环配合数组遍历子节点查找元素
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]);
上一篇:JavaScript cookie的设置获取删除详解
栏 目:JavaScript代码
下一篇:js 拖拽翻页实现代码
本文标题:JS 使用for循环遍历子节点查找元素
本文地址:http://www.codeinn.net/misctech/30643.html






