开发者

How to distinguish between live and non-live NodeList collections?

开发者 https://www.devze.com 2023-01-09 12:11 出处:网络
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 sta开

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 sta开发者_如何学Gotic 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")?


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.


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

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

(in FF/Chrome)

0

精彩评论

暂无评论...
验证码 换一张
取 消