Locates by the class attribute, returns a reference to elements with the specified class attribute value in the document as an HTMLCollection.
<divclass="t2">D2</div><divclass="t2">D3</div><scripttype="text/javascript">var t2List = document.getElementsByClassName("t2"); console.log(t2List);// HTMLCollection(2) [div.t2, div.t2]// Traverse using a for loopfor(let i=0,n=t2List.length;i<n;++i) console.log(t2List[i]);// Since HTMLCollection's prototype does not have a forEach method, traversal requires using Array's prototype and invoking the call method to bind the object instance and pass parametersArray.prototype.forEach.call(t2List,v=> console.log(v));// Since HTMLCollection's prototype does not have a map method, traversal also requires using Array's prototype and invoking the call method to bind the object instance and pass parametersArray.prototype.map.call(t2List,v=> console.log(v));</script>
Locates by the name attribute, returns a reference to elements with the specified name attribute value in the document as a NodeList.
<divname="t3">D4</div><divname="t3">D5</div><scripttype="text/javascript">var t3List = document.getElementsByName("t3"); console.log(t3List);// NodeList(2) [div, div]// Can directly use forEach for traversal t3List.forEach(v=> console.log(v));// Since NodeList's prototype does not have a map method, scenarios involving map also require using Array's prototype and invoking the map method through call to bind the object instance and pass parametersArray.prototype.map.call(t3List,v=> console.log(v));</script>
<!DOCTYPEhtml><html><head><title>JavaScript Selectors</title><metacharset="utf-8"></head><body>```html
<divid="t1">D1</div><divclass="t2">D2</div><divclass="t2">D3</div><divname="t3">D4</div><divname="t3">D5</div><pclass="t4">P6</p><pclass="t4">P7</p><div><divclass="t5">D8</div></div><div><divid="t6">D9</div><div>D10</div><div>D11</div></div></body><scripttype="text/javascript">var t1 = document.getElementById("t1"); console.log(t1);// <div id="t1">D1</div> console.log(Object.prototype.toString.call(t1));// [object HTMLDivElement] console.log("");var t2List = document.getElementsByClassName("t2"); console.log(t2List);// HTMLCollection(2) [div.t2, div.t2]// Loop through using a for loopfor(let i=0,n=t2List.length;i<n;++i) console.log(t2List[i]);// The HTMLCollection prototype doesn't have a forEach method, so we need to use Array's prototype forEach and bind the object instance and pass parameters using callArray.prototype.forEach.call(t2List,v=> console.log(v));// The HTMLCollection prototype doesn't have a map method, so we also need to use Array's prototype forEach and bind the object instance and pass parameters using callArray.prototype.map.call(t2List,v=> console.log(v)); console.log("");var t3List = document.getElementsByName("t3"); console.log(t3List);// NodeList(2) [div, div]// You can directly use forEach for traversal t3List.forEach(v=> console.log(v));// The NodeList prototype doesn't have a map method, and using map also requires Array's prototype map and bind the object instance and pass parameters using callArray.prototype.map.call(t3List,v=> console.log(v)); console.log("");var t4List = document.getElementsByTagName("p"); console.log(t4List);// HTMLCollection(2) [p, p]Array.prototype.forEach.call(t4List,function(v){console.log(v);});Array.prototype.map.call(t4List,function(v){console.log(v);}); console.log("");var t5 = document.querySelector("div > .t5"); console.log(t5);// <div class="t5">D8</div> console.log(Object.prototype.toString.call(t5));// [object HTMLDivElement] console.log("");var t6List = document.querySelectorAll("#t6 ~ div"); console.log(t6List);// NodeList(2) [div, div] t6List.forEach(function(v){console.log(v);});Array.prototype.map.call(t6List,function(v){console.log(v);}); console.log("");</script></html>