如何区分活动 NodeList 集合和非活动 NodeList 集合?

发布于 2024-09-11 04:10:20 字数 193 浏览 2 评论 0原文

document.getElementsByTagName('div') 和 document.querySelectorAll('div') 都返回 NodeList 集合。唯一的区别是第一个方法返回实时集合,第二个方法返回静态集合。

问题是 - 是否有机会仅通过检查这些对象来区分一个对象与另一个对象(即 - 不尝试添加/删除某些项目来测试“活跃度”)?

Both document.getElementsByTagName('div') and document.querySelectorAll('div') return NodeList collection. The only difference is that first method returns live-collection and second one - a static one.

The question is - is there any opportunity to distinguish one object from another only via inspecting these objects (i.e - not trying to add/remove some items to test "liveness")?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灰色世界里的红玫瑰 2024-09-18 04:10:20

NodeList 接口不知道其死亡或活动状态。

interface NodeList {
  Node item(in unsigned long index);
  readonly attribute unsigned long length;
};

它只包含一个属性 length 和一个方法 item,所以恐怕目前无法在不操作 DOM 并查看效果的情况下确定对象是否处于活动状态。

The NodeList interface is agnostic of its dead or live status.

interface NodeList {
  Node item(in unsigned long index);
  readonly attribute unsigned long length;
};

It only contains a property length, and a method item so I'm afraid it's currently not possible to determine if an object is live without manipulating the DOM and seeing the effects.

傲影 2024-09-18 04:10:20
a=document.querySelectorAll('a');
b=document.getElementsByTagName('a');

a.toString() == "[object NodeList]"
b.toString() == "[object HTMLCollection]"

(在 FF/Chrome 中)

a=document.querySelectorAll('a');
b=document.getElementsByTagName('a');

a.toString() == "[object NodeList]"
b.toString() == "[object HTMLCollection]"

(in FF/Chrome)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文