0

我想获得特定的子节点(即我只想要 div)。我怎样才能做到这一点。这是我的代码

<html>
<head>

</head>
<body>
    <div id="test">
        <input type="text">
        <div class="nod1"></div>
        <input type="text">
        <div class="nod2"></div>
    </div>
</body>
    <script>
    var tst=document.getElementById('test');
    var childrens=tst.children;
    for(var i=0; i<childrens.length; i++){
        console.log(childrens[i]);
    }
    </script>
</html>
4

3 回答 3

0
 <script>
    var tst = document.getElementById('test');
    var childrens = tst.children;
    for (var i = 0; i < childrens.length; i++) {
        if (childrens[i].tagName == "DIV") {
            alert("its a div");
         }

    }
</script>
于 2013-05-13T05:56:32.627 回答
0

像这样试试

for(var i=0; i<childrens.length; i++){
    console.log(childrens[i].className);
}
于 2013-05-13T05:48:40.110 回答
0

做这个,

<script>
var tst=document.getElementById('test');
var childrens=tst.children;
for(var i=0; i<childrens.length; i++){
    if(childrens[i].tagName=='div')
        console.log(childrens[i]);
}
</script>
于 2013-05-13T05:51:47.677 回答