我有一个链接列表。我想为每个链接编写代码,如果该链接的第一个子代不是标签...则执行一些代码。
到目前为止,我有:
links.forEach((link) => {
if(link.firstChild /* Not sure what to put here */) {
// Do some code here
}
});
如果我了解正确性,则想了解某个元素是否为锚标记,则可以编写以下if条件:
if (!link.firstChild.matches('a') { /* do something */ }
// OR
if (link.firstChild.tagName !== 'A') { /* do something */ }
但是,如果要访问,.firstChild
则由于HTML中的空白,您可能会以文本节点结尾。.firstElementChild
避免这种情况可能更安全。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句