Home >Web Front-end >JS Tutorial >What is DOM in JavaScript? How to access dom node

What is DOM in JavaScript? How to access dom node

伊谢尔伦
伊谢尔伦Original
2017-07-20 13:20:541993browse

DOM is the abbreviation of Document object Model. The document object model is a document that expresses XML or HTML in the form of tree nodes. Using DOM methods and properties, you can access, modify, delete any element on the page, and you can also add an element. DOM is a language-independent API that can be implemented in any language, including Javascript

Look at the text below.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html> 
 <head> 
  <title>My page</title> 
 </head> 
 <body> 
  <p class="opener">first paragraph</p> 
  <p><em>second</em> paragraph</p> 
  <p id="closer">final</p> 
 </body> 
</html>

Let’s take a look at the second paragraph


<p><em>second</em> paragraph</p>

You can see that this is a p tag. It is included in the body tag. So body is the parent node of p, and p is the child node. The first and third paragraphs are also child nodes of the body. They are all sibling nodes of the second paragraph. This em tag is a child node of the second segment p. Therefore p is its parent node. The parent-child node relationship can depict a tree-like relationship. So it’s called DOM tree.

Accessing DOM nodes

When we want to validate the form or change the image, we need to know how to access the elements (element.). There are many ways to get elements.
Document node
We can access the current document through document. We can use firebugs (Firefox plug-in) to view the document's properties and methods.
All nodes have the attributes nodeType, nodeName, nodeValue. Let’s take a look at the nodeType of document


document.nodeType;//9

There are 12 node types in total. document is 9. Commonly used ones are element (element: 1), attribute (attribute: 2), and text (text: 3).
Nodes also have names. for HTML tags. The node name is the label name. The name of the text node (text) is #text. The name of the document node (document) is #document.

nodes also have values. For text nodes, the value is the text. The value of document is null
documentElement
XML will have a ROOT node to wrap the document. for HTML documents. The ROOT node is the html tag. Access the root node. You can use the properties of documentElement.


document.documentElement;//<html> 
document.documentElement.nodeType;//1 
document.documentElement.nodeName;//HTML 
document.documentElement.tagName;//对于element,nodeName和tagName相同

Child Nodes
In order to determine whether it contains child nodes, we can use the following method


document.documentElement.hasChildNodes();//true

HTML has two child nodes.


document.documentElement.childNodes.length;//2 
 
document.documentElement.childNodes[0];//<head> 
 
document.documentElement.childNodes[1];//<body>

You can also access the parent node through the child node


document.documentElement.childNodes[1].parentNode;//<html>

We assign the reference of the body to the variable


var bd = document.documentElement.childNodes[1]; 
bd.childNodes.length;//9

Let’s take a look at the structure of the body


 
 

first paragraph

<p><em>second</em> paragraph</p>

final


Why is the number of child nodes 9?
First there are 4 P's and a comment, a total of 4.
4 nodes include 3 blank nodes. That’s 7.
The 8th blank node between body and first p.
The ninth one is the blank node between the comment and 36cc49f0c466276486e50c850b7e4956.
There are 9 nodes in total.


Quick access to DOM

You can access any node in the document through childNodes, parentNode, nodeName, nodeValue and attributes. But in actual application, text nodes are quite annoying. If the text changes, it may affect the script. Also, if the DOM tree is deep enough, it is indeed inconvenient to access it. Fortunately, we can access nodes in a more convenient way. These methods are


getElementsByTagName()
getElementsByName()
getElementById()

First let’s talk about getElementsByTagName()
Get a collection of html elements through a tag name. The example is as follows


document.getElementsByTagName(&#39;p&#39;).length;//3

Because what is returned is a collection, we can access it in the form of an array subscript or through the item method. To compare, it is recommended to use the array access method. Something simpler.


document.getElementsByTagName(&#39;p&#39;)[0];// <p class="opener"> 
document.getElementsByTagName(&#39;p&#39;).item(0);//和上面的结果一样 
 
document.getElementsByTagName(&#39;p&#39;)[0].innerHTML;//first paragraph

To access the attributes of an element, you can use the attributes collection. But the easier way is to access it directly as a property. Look at an example


document.getElementsByTagName(&#39;p&#39;)[2].id;//closer

It should be noted that the class attribute cannot be used normally. . Use className. Because class is a reserved word in the JavaScript specification.


document.getElementsByTagName(&#39;p&#39;)[0].className;//opener

We can access all elements of the page using the following method


<span style="color: #ff0000;">document.getElementsByTagName(&#39;*&#39;).length;//9</span>

Note: This is not supported in earlier versions of IE the above method. Can be replaced with document.all. IE7 already supports it, but all nodes are returned, not just element nodes.

Siblings, Body, First, Last Child
nextSibling and previousSibling are two more convenient ways to access the DOM. Used to access adjacent nodes. The example is as follows


var para = document.getElementById(&#39;closer&#39;) 
para.nextSibling;//"\n" 
para.previousSibling;//"\n" 
para.previousSibling.previousSibling;//<p> 
para.previousSibling.previousSibling.previousSibling;//"\n" 
para.previousSibling.previousSibling.nextSibling.nextSibling;// <p id="closer">

body is used to access the body element.


document.body;//<body>

firstChild and lastChild. firstChild is the same as childNodes[0]. lastChild is the same as childNodes[childNodes.length - 1].

The above is the detailed content of What is DOM in JavaScript? How to access dom node. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn