HTMLCollection与Nodelist
#javascript #dom #htmlcollection #nodelist

dom到meotheyoreo§a§to| at ||||||消息|°至|°至|°至| |。 §A| AT | at |°at | post | to | to | a ||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||¶à§a| to | to | || | | | | | | | |

dom -dom -à °°¾°°à §§—® htmlCollection‾€¥ 。 à……nodeList - €€¾—¥ htmlcollectionà -nodeListà°°®®¾¾ -¨ ‾基§ 。 €¥à¾——€²à§à§à§à ðµ

à à§à§¿ /p>

htmlCollection <强> nodelist à ¾类似阵列的阵列!

htmlCollection

getElementsByClassName()à -à àj® °§ ®°¾¾——® ! à§à °à§à

à书籍清单ৠ¦†à¦®à¦¾à¦¦à§‡à¦°à¦•à§‡ প্রত্যেকটঠ¾列表项目访问‰�I°§à§à§à§à§ > getElementsbytybytagname()à§à§ - ¦¿ দেখবো?🤔

index.html

   <ul>
      <li>Bangla</li>
      <li>English</li>
      <li>Biology</li>
      <li>Physics</li>
    </ul>

list.js

const list = document.getElementsByTagName("li");
console.log(list);

Output
//HTMLCollection(4) [li, li, li, li]

à ¦¤à§‡ পারলাম HTMLCollection এ শুধু HTML elements গু à§商店 �°à§°°à§à getElementsbythame() �°à §§——¾ �(文本)à �—¾¾¥¶ ¾à§dom元素实际元素‡ কোথাই একটি element DOM এ located আছে সেঠ‾

nodelist

dom-domà§à节点

Image description

à

1)元素节点
2)文本节点
3)属性节点
4)评论节点

htmlCollection ৗ§ 不同类型的节点 °¾

à§à§à „

index.html

<div id="box">
  I'm Text Node
  <div>I'm Element Node 1</div>
  <div>I'm Element Node 2</div>
</div>

text.js

const box = document.getElementById("box");
console.log(box.childNodes);

Output 
// NodeList(5) [text, div, text, div, text]

à °°°«à最后一রা দেখতে পারছি, NodeList tag গুলোঠ°氨基à§存储

nodelistà主要信息(元素节点)à ¾——见°¾很容易易于不同类型的节点€¾°¬ à°¾¾¾¾—— ! â€à§à¥¶

à ‾ptag - 文字â€à§à¥¶文字à§à§à§à§à颜色¾-..k./。 িà¦à¦¾à¦¬à§‡ access করব??

htmlCollection htmlCollection ¾ -à ‿¾ - nodeList ¾? à§à§ -nodeList - n?¿歌à§color的à节点存储

index.html

   <div id="container">
      <p>First paragraph</p>
      Some text between paragraphs.
      <p>Second paragraph</p>
    </div>

text.js

 // Get the container element
    var container = document.getElementById("container");

 // Get a NodeList containing all child nodes of the container
      var nodes = container.childNodes;

      // Loop through the NodeList
      nodes.forEach(function (node) {
        // Check if the node is a text node
        if (node.nodeType === Node.TEXT_NODE) {
          // Change the text color of text nodes
          node.parentNode.style.color = "red";
        }
      });

à ¾¾¾¾ à§复合物¾!

querySelectorall()àrym> °°°§—®的

 const list = document.querySelectorAll("li");
 console.log(list);

output
//NodeList(4) [li, li, li, li]

à ‾基¾ 。 ! nodeList - ¶ ÖTHIMAS§

差异 - nodeList - °°相似性 -

১)htmlCollection ¾°°
)htmlCollectionà! €¨¾

à

  • ¶à§drig- à �— ® /¾ ¾ -

১)foreach
)条目
৩)键
৪)值

à|:|® à§à|¥à|€à§²à§htmlCollectionà|à|à|à|ªà|ª

  • à ()à ¾°£ ®°¾¾htmlCollectionâ最新更新 - ¾ querySelectorall()àr getelementsByname() ° > querySelectorall()àr getElementsByname() ¥¶

htmlCollection

const boxWrapper = document.getElementById("box-wrapper");
const boxes = boxWrapper.getElementsByClassName("box");
console.log(boxes.length); // 4

const div = document.createElement("div");
div.classList.add("box");
div.innerHTML = "box 5";
boxWrapper.appendChild(div);
console.log(boxes.length); // 5

nodelist

const boxes = document.querySelectorAll(".box");
console.log(boxes.length); // 4

const div = document.createElement("div");
div.setAttribute("class", "box");
div.innerHTML = "box 5";
boxWrapper.appendChild(div);

console.log(boxes.length); // 4
  • 最后,孩子们à§htmlcollectionà ! ৾ €à§所有类型的节点访问��°°

index.html

<div id="container">
    <p>First paragraph</p>
    Some text between paragraphs.
    <p>Second paragraph</p>
</div>

text.js

var container = document.getElementById('container');

var children = container.children; 
// Only includes <p> elements

var childNodes = container.childNodes; 
// Includes <p>, text, and text nodes

htmlCollection র ও কিছু benefits আছে তাই কখন HTMLCollection à¦¬à §§ €¹ ¦‰à¦ªà¦°à¥¤

à ৠ-

১)htmlCollection - °nodeList - 所有类型的节点存储

à§) getElementsByClassName()àm à!€¾—¦ >ৠstatic à