P粉9044059412023-08-25 17:17:22
You use array as object, getElementbyId
and
getElementsByClassName
is:
getElementbyId
will return an Element object < /a> or null if no element with that ID is foundgetElementsByClassName
will return a live HTMLCollection< /a>, which may be of length 0https://www.w3.org/ TR/2008/WD-html5-20080610/dom.html#getelementsbyclassname
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 have style < /code> 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 the style
property.
P粉5205457532023-08-25 16:48:41
Your getElementById
code works because the ID must be unique, so the function always returns only one element (or null if not found)
).
However, these methods
getElementsByClassName
,
getElementsByName
,
getItemByTagName
, and
getElementsByTagNameNS
Returns an iterable collection of elements.
Method names provide hints: getElement
implies singular, while getElements
implies plural.
MethodsquerySelector
a> also returns a single element, and querySelectorAll code>
returns an iterable collection.
The iterable collection can be NodeList
or HTMLCollection
一个>a>.
getElementsByName
and querySelectorAll
both specify to return the node list
; othersgetElementsBy *
Method specifies the return of a < code>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 read style
.
In other words: NodeList
or HTMLCollection
does not have style
; only Element
has style
.
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 use
Array.from; then you can use
forEach and other array methods such as iteration methods
:
Array.from(document.getElementsByClassName("myElement"))
.forEach((element) => element.style.size = "100px");
In older browsers that do not support Array.from or the iteration method, you can still use
Array.prototype.slice.call.
Then you can iterate over it like a real array:
var elements = Array.prototype.slice
.call(document.getElementsByClassName("myElement"));
for(var i = 0; i < elements.length; ++i){
elements[i].style.size = "100px";
}
You can also iterate over the
or HTMLCollection
itself, but note that in most cases these collections are live
(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 forEach
in modern browsers:
You can also use a simple document.querySelectorAll(".myElement")
.forEach((element) => element.style.size = "100px");
loop:
Narration: var elements = document.getElementsByClassName("myElement");
for(var i = 0; i < elements.length; ++i){
elements[i].style.size = "100px";
}
.childNodes generates a
live< /a> NodeList and .children
Generates a
live HTMLCollection, so these two getters also need to be handled with care.
, that make DOM queries shorter and create an abstraction layer over "an element" and a "collection of elements":
$(".myElement").css("size", "100px");