获取特定的子节点类名 javascript

我想获得特定的子节点(即我只想要 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>
stack overflow Get particular child node class name javascript
原文答案

答案:

作者头像

Try like this

for(var i=0; i<childrens.length; i++){
    console.log(childrens[i].className);
}
作者头像

Do this,

<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>
作者头像
 <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>