When getting native DOM elements, it mainly involves these Dom APIs (linked to living standard):
Node
and corresponding setsNodeList
Element
(Inheritance Node
) and corresponding setsHTMLCollection
Document
(Inherited Node
)
Note: The plan supersedes NodeList
and HTMLCollection
is Elements
not currently widely implemented
Basic knowledge--NodeList v.s. htmlcollection
In different versions of the browser, if you call the DOM method that gets the multi-element (GetElement ... ()), some will get NodeList
(many for the old browser), some will get HTMLCollection
(more for the new browser). The methods of using node interface, such as childnodes, are usually nodelist, and other interface methods are possible HTMLCollection
. So it is necessary to understand the difference between the two.
There is a good question and answer on the StackOverflow about these two types of differences.
In fact, as long as the first look at the living standard in the two types of IDL, you can guess the approximate. NodeList
the IDL is as follows:
Interface NodeList { Getter Node item (unsigned long index); ReadOnly attribute unsigned long length; iterable<node>;};
And HTMLCollection
the IDL is as follows:
Interface Htmlcollection { readonly attribute unsigned long length; Getter Element? Item (unsigned long index); Getter Element? Nameditem (domstring name);};
Same point:
- They all have
length
attributes.
- All have elements of getter, called
item
Different points:
NodeList
The element is Node
, HTMLCollection
the element is Element
.
Element
Inherited from Node
, is Node
a kind of, in HTML, it is generally an HTML element (for example, such as <p>
<a>
a tag created by the object). and Node
as a parent class, there Element
are some other subclasses, such as the text within the HTML element corresponding to the document, corresponding to the Text
Document
annotation Comment
.
HTMLCollection
, only Element
, and NodeList
in can have Element
,, and Text
Comment
so many elements. Ordinarily, if the Get element returns the list only Element
, then these two kinds of not much difference, but in fact many times the browser will parse the HTML text to get Text
and put in the list to Comment
put back. For example, the following section of code
<div> <!--Comment-- <p>this is Some text</p></div>
If the sub-elements of this div are returned in the list, then the NodeList
browser can give a maximum of 5 elements (different browsers may be different) if it is returned.
- A break between a
<div>
and a comment and a space (or tab) as text node (yes, a blank symbol between tags can also be parsed as text node)
- Note as comment node
- Comment and break
<p>
between and spaces (or tab) as text node
p
As Element
</p>
And </div>
between the break and space (or tab) as text node
As a result, NodeList
there may be a lot of text node and comment node that are not required for general DOM operations to be processed. And that's HTMLCollection
a lot simpler, only <p>
one element, and that's the result of being more intuitive to most people.
HTMLCollection
There is also a namedItem
way to get the elements quickly. Let's say that there is an HTML:
<div> <!--Comment- <p>this is Some text</p> </div>
So assuming that the sub-elements of this div are composed HTMLCollection
, called list
, then use list.namedItem("test")
can directly get inside the img
elements.
Lookup order reference living standard, but in reality not all browsers follow standards. For example, if there are multiple elements with the same ID or name, as long as the first one is returned, Chrome and opera will return them in a htmlcollection or nodelist, see MDN.
There are a few points that you can't see from IDL.
- These two classes are all "live". Manipulating the elements will be reflected in the DOM in real time (and therefore expensive if multiple DOM operations are performed directly on such lists at once).
item
And namedItem
all can be []
called by the abbreviation, and some browsers also support ()
the use of abbreviations to call (that is, can, list[index]
list[key]
or list(index)
, list(key)
), and directly with the dot notation call namedItem
(for example list.key
).
- Some browsers support
NodeList
calls namedItem
or indirectly through []
, ()
dot notation, namedItem
but because of the different browser support, it is best not to NodeList
do this kind of operation.
- IE8 and the following versions of the browser, comments belong
HTMLCommentElement
, counted Element
, and therefore appear in HTMLCollection
.
"Turn" native Dom inquiry--NodeList v.s. htmlcollection