What do the querySelectorAll and getElementsBy* methods return?
P粉060112396
P粉060112396 2023-08-24 19:45:56
0
2
443
<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>
P粉060112396
P粉060112396

reply all(2)
P粉904405941

You use array as object, getElementbyId and getElementsByClassName is:

getElementsByClassName

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 have style 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粉520545753

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 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

NodeList

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,

NodeList

provides some iteration methods, such as forEach in modern browsers:

document.querySelectorAll(".myElement")
  .forEach((element) => element.style.size = "100px");
You can also use a simple

for

loop:

var elements = document.getElementsByClassName("myElement");

for(var i = 0; i < elements.length; ++i){
  elements[i].style.size = "100px";
}
Narration:

.childNodes generates a live NodeList and .children Generates a live HTMLCollection, so these two getters also need to be handled with care.

There are libraries, such as
jQuery

, that make DOM queries shorter and create an abstraction layer over "an element" and a "collection of elements":

$(".myElement").css("size", "100px");

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!