我遍历 jQuery 对象列表并检查 nextSibling 属性是否为 null
。不幸的是,html 注释被视为对象类型的同级。我怎样才能捕获这个?我需要一个为 obj.nextSibling == null || 触发的 if 语句obj.nextSibling == 注释
最佳答案
如何编写一个函数来执行此操作,该函数仅返回元素(即node.nodeType === 1
)
function nextElement(node) {
while (node = node.nextSibling) if (node.nodeType === 1) return node;
return null;
}
它的作用是,
// while
node = node.nextSibling // get next sibling, if falsy break while
if (node.nodeType === 1) // if it is an Element, return it
// else go back to while
return null; // if we get here, next sibling was null
您可以看到 nodeType 的不同值列表 here on MDN .
关于javascript - Sibling 不为 null,因为注释算作对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17110596/