这篇文章主要介绍了JS 使用for循环配合数组遍历子节点查找元素
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
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]); |