DOM (Document Object Model), Document Object Model (DOM) is a programming interface for HTML and XML documents. It provides a structured representation of a document and defines a way to access this structure from within a program to change the structure, style, and content of the document. DOM parses a document into a structured collection of nodes and objects (objects that contain properties and methods). In short, it links web pages with scripting or programming languages. ——————— Quoted from the official website
2. DOM node tree
JS operates the webpage, which will turn the entire webpage into a node tree, and operate the node tree through DOM, so as to realize the operation of the webpage. The node tree is classified as follows:
type
describe
Node Value (NodeValue)
Node name (NodeName)
Node type (NodeType)
document node
Document //The entire document
Null
#document
9
element node
Element //html tag
Null
HTML
1
text node
Text //text
Text content (spaces and newlines are also text content)
#Text
3
comment node
Comment //Comment
N/A
N/A
N/A
attribute node
Attr //attribute
attribute value
attribute name
2
(1) Node object properties
1) Node attributes
Attributes
describe
parentNode
Returns the parent node of the current node
childNodes
Returns all child nodes of the current node
children
Returns all element children of the current node
firstChild
Returns the first child node of the current node
firstElementChild
Returns the first element child of the current node
lastChild
Returns the last child node of the current node
lastElementChild
Returns the last element child of the current node
nextSibling
Returns the next node of the current node
nextElementSibling
Returns the next element node of the current node
prevSibling
Returns the previous node of the current node
prevElementSibling
Returns the previous element node of the current node
document.body
Navigate to the body tag
(2) Get node elements
1) Obtain by ID
example
describe
Value method
document.getElementById( “ID name” )
Return a node object, you can get the content by object value
document.getElementById( "ID name" ).Key name
2) Obtain according to Name
example
describe
Value method
document.getElementByName( “Name” )
Returns a list of nodes, the content can be obtained by subscripting the array
document.getElementsByName( "name" )[subscript]
3) Obtain according to ClassName
example
describe
Value method
document.getElementsByClassName(“类名”)
Returns a list of nodes, the content can be obtained by subscripting the array