如何快速检查 NodeList(从父节点派生)是否包含多个节点?
例如,如果我有一些 div:
<div class="parent">
<div></div>
<div></div>
<div><span></span></div>
</div>
我想检查“parent”是否包含多个 div,如下所示:
var parentDiv = document.querySelector(".parent");
var children = parentDiv.getElementsByTagName("div");
if (children > 1) {
alert("more than 1 child");
}
else {
alert("less than 1 child");
}
console.log(children);
但是,这不起作用。有没有简单快捷的方法?也许某种数组原型(prototype)之类的?
最佳答案
使用.length
属性:
if (children.length > 1) {
alert("more than 1 child");
}
关于javascript - 快速检查 NodeList 是否有多个节点的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24220171/