What do the querySelectorAll and getElementsBy* methods return?
P粉060112396
2023-08-24 19:45:56
<p><code>getElementsByClassName</code> (and similar functions like <code>getElementsByTagName</code> and <code>querySelectorAll</code>) works with <code>getElementById</code> Is it the same way? Return an array of elements? </p>
<p>The reason I ask is because I'm trying to change the style of all elements using <code>getElementsByClassName</code>. see below. </p>
<pre class="brush:php;toolbar:false;">//doesn't work
document.getElementsByClassName('myElement').style.size = '100px';
//works
document.getElementById('myIdElement').style.size = '100px';</pre></p>
You use array as object,
getElementbyId
andgetElementsByClassName
is:getElementbyId
will return an Element object or null if no element with that ID is foundgetElementsByClassName
will return a live HTMLCollection, which may be of length 0getElementsByClassName
https://www.w3.org/ TR/2008/WD-html5-20080610/dom.html#getelementsbyclassname
Get ElementById
https://developer.mozilla.org/en- US/docs/Web/API/Document/getElementById
Include the following lines in your code:
will not work as expected because
getElementByClassName
will return an array, and that array does not havestyle properties that you can Each
element
is accessed by iteration.That's why the function
getElementById
works for you, the function will return the direct object. Therefore, you will be able to access thestyle
property.Your
getElementById
code works because the ID must be unique, so the function always returns only one element (ornull if not found)
).However, these methods
getElementsByClassName
,getElementsByName
,getItemByTagName
, andgetElementsByTagNameNS
Returns an iterable collection of elements.Method names provide hints:
getElement
implies singular, whilegetElements
implies plural.Methods
querySelector
a> also returns a single element, andquerySelectorAll code>
returns an iterable collection.The iterable collection can be
NodeList
orHTMLCollection
一个>a>.getElementsByName
andquerySelectorAll
both specify to return thenode list
; othersgetElementsBy *
Method specifies the return of a HTMLCollection, but please note that some browser versions implement this differently.These two collection types do not provide the same properties as elements, nodes, or similar types; that is why from
document.getElements
…(
…)
Reason for failure to readstyle
. In other words:NodeList
orHTMLCollection
does not havestyle
; onlyElement
hasstyle
.These "array-like" collections are lists of zero or more elements that you need to iterate over to access them. While you can iterate over them just like arrays, note that they are not the same as " https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference /Global_Objects/Array” rel="noreferrer">arrays
.
In modern browsers you can useArray.from
; then you can use
forEach and otherarray methods such as iteration methods
: In older browsers that do not support Array.from or the iteration method, you can still useArray.prototype.slice.call
NodeList. Then you can iterate over it like a real array:
You can also iterate over theor
NodeListHTMLCollection
itself, but note that in most cases these collections arelive
(MDN Document , DOM Specification), that is, they are updated as the DOM changes. So if you insert or remove elements while looping, make sure you don't accidentally skip some elements or create an infinite loop . MDN documentation should always indicate whether a method returns a live collection or a static collection. For example,provides some iteration methods, such as
You can also use a simple
forforEach
in modern browsers:loop:
Narration:
.childNodes
There are libraries, such asgenerates a
live NodeList and.children
Generates a
live HTMLCollection, so these two getters also need to be handled with care.jQuery
, that make DOM queries shorter and create an abstraction layer over "an element" and a "collection of elements":