DOM 树是HTML网页结构的树状表示,网页中的每个标签、文本、属性都是树中的一个节点,JavaScript可以通过DOM树操作网页内容。 The DOM (Document Object Model) tree is a hierarchical, tree-like representation of the structure of an HTML or XML document. Each HTML element, attribute, and piece of text is a node in the tree. JavaScript uses the DOM tree to access, modify, add, or delete content on a web page.
How many ways to access an element in JavaScript?
6种方式
document.getElementById("id") – by ID (returns one element)
document.getElementsByClassName("class") – by class name (returns a collection)
document.getElementsByTagName("tag") – by tag name (returns a collection)
document.getElementsByName("name") – by name attribute (used in forms)
document.querySelector("selector") – by CSS selector (returns the first match)
document.querySelectorAll("selector") – by CSS selector (returns all matches)