How to distinguish between live and non-live NodeList collections?
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)
精彩评论