我想获得特定的子节点(即我只想要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>
答案 0 :(得分:0)
试试这个
for(var i=0; i<childrens.length; i++){
console.log(childrens[i].className);
}
答案 1 :(得分: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>
答案 2 :(得分: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>