Home > Article > Web Front-end > Here are some English Q&A question titles that match the content of your article: More concise title: * What\'s the Difference Between HTMLCollections, NodeLists, and Arrays of Objects in the DOM? * DOM Collections: HTMLCollections, NodeLists, and Arrays - What\'s t
When working with the DOM, three key types of collections come into play: HTMLCollections, NodeLists, and arrays of objects. Each of these collections serves a specific purpose and has its own unique characteristics.
HTMLCollections represent collections of HTML elements that match a specific tag name. They are returned by the getElementsByTagName() method of the Document object. HTMLCollections are live, meaning they automatically reflect any changes made to the DOM. They also provide direct access to individual elements by index.
NodeLists, on the other hand, are collections of any type of Node (including HTML elements, text nodes, and comments). They are returned by various DOM methods, such as querySelectorAll() and childNodes(). NodeLists are static, meaning they do not reflect changes in the DOM unless explicitly updated.
jQuery objects are not directly related to HTMLCollections or NodeLists. jQuery objects are JavaScript objects that encapsulate DOM selections. They provide a convenient interface for manipulating DOM elements and enhancing their functionality using jQuery's rich API.
jQuery selections can include elements, text nodes, or any other type of Node. Like NodeLists, jQuery selections are static. However, they can be converted to live HTMLCollections using jQuery's $(...).live() method.
In addition to HTMLCollections and NodeLists, you can also create arrays of objects in JavaScript. For example, you can store DOM elements in an array as follows:
<code class="javascript">const elements = [document.getElementById("myElement1"), document.getElementById("myElement2")];</code>
Arrays in JavaScript are dynamic and do not reflect changes made to the DOM. They also do not provide access to specific methods like HTMLCollections or jQuery objects.
The following script demonstrates the key differences between these collection types:
<code class="javascript">$(function(){ console.log('[123,"abc",321,"cba"]=',[123,"abc",321,"cba"]); console.log('{123:123,abc:"abc",321:321,cba:"cba"}=',{123:123,abc:"abc",321:321,cba:"cba"}); console.log('Node=',Node); console.log('document.links=',document.links); console.log('document.getElementById("myTable")=',document.getElementById("myTable")); console.log('document.getElementsByClassName("myRow")=',document.getElementsByClassName("myRow")) console.log('document.getElementsByTagName("td")=',document.getElementsByTagName("td")); console.log('$("#myTable")=',$("#myTable")); console.log('$("td")=',$("td")); });</code>
This script logs the following output:
[123,"abc",321,"cba"]=[123, "abc", 321, "cba"] {123:123,abc:"abc",321:321,cba:"cba"}=Object { 123=123, abc="abc", 321=321, more...} Node= undefined document.links= HTMLCollection[a #, a #] document.getElementById("myTable")= <table> document.getElementsByClassName("myRow")= HTMLCollection[tr.myRow, tr.myRow] document.getElementsByTagName("td")= HTMLCollection[td, td, td, td] jQuery("#myTable")= [table#myTable] jQuery("td")= [td, td, td, td]
The above is the detailed content of Here are some English Q&A question titles that match the content of your article: More concise title: * What\'s the Difference Between HTMLCollections, NodeLists, and Arrays of Objects in the DOM? * DOM Collections: HTMLCollections, NodeLists, and Arrays - What\'s t. For more information, please follow other related articles on the PHP Chinese website!