Home > Web Front-end > JS Tutorial > JavaScript HTML DOM navigation (summary sharing)

JavaScript HTML DOM navigation (summary sharing)

WBOY
Release: 2022-08-05 17:13:28
forward
1625 people have browsed it

This article brings you relevant knowledge about javascript, which mainly introduces issues related to HTML DOM navigation. Let’s take a look at it together. I hope it will be helpful to everyone.

JavaScript HTML DOM navigation (summary sharing)

[Related recommendations: javascript video tutorial, web front-end

JS HTML DOM navigation

With the HTML DOM, you can use node relationships to navigate the node tree.

DOM Node

According to the W3C HTML DOM standard, Everything in an HTML document is a node:

  • The entire document is a document node
  • Each HTML element is an element node
  • The text within an HTML element is a text node
  • Each HTML attribute is an attribute node
  • All comments are comments Node
    JavaScript HTML DOM navigation (summary sharing)
    With the HTML DOM, all nodes in the node tree are accessible through JavaScript.

Be able to create new nodes, and also modify and delete all nodes.

Node relationship

The nodes in the node tree have a certain hierarchical relationship with each other.

  • The terms (parent, child and sibling, parent, child and sibling) are used to describe these relationships
    • In the node tree, the top node is called the root (root node)
    • Every node has a parent node, except the root (the root node has no parent node)
    • Node can have a certain number of children
    • Siblings (brothers or sisters) refer to Nodes with the same parent

eg:


   
       <title>DOM 教程</title>
   

  
       <h1>DOM 第一课</h1>
       <p>Hello world!</p>
   

Copy after login

JavaScript HTML DOM navigation (summary sharing)

从以上的 HTML 中您能读到以下信息:

-  是根节点
-  没有父
-  是  和  的父
-  是  的第一个子
-  是  的最后一个子
**同时:**

-  有一个子:<title>
- <title> 有一个子(文本节点):"DOM 教程"
- </title>
</title> 有两个子:<h1> 和 </h1><p>
- </p><h1> 有一个子:"DOM 第一课"
- </h1><p> 有一个子:"Hello world!"
- </p><h1> 和 </h1><p> 是同胞</p>
Copy after login

Navigate between nodes

Through JavaScript, you can navigate between nodes using the following node properties :

  • parentNode
  • childNodes [nodenumber]
  • firstChild
  • lastChild
  • nextSibling
  • previousSibling

Child nodes and node values

## A common mistake in #DOM processing is assuming that element nodes contain text.

Example:

<title>DOM 教程</title>
Copy after login
(in the above example) element node

does not contain text. It contains a text node with the value "DOM Tutorial".

    The value of a text node can be accessed through the node's innerHTML attribute:
  1. var myTitle = document.getElementById("demo").innerHTML;
    Copy after login
    Accessing the innerHTML attribute is equivalent to accessing the nodeValue of the first child node:
  1. var myTitle = document.getElementById("demo").firstChild.nodeValue;
    Copy after login
    You can also access the first child node like this:
  1. var myTitle = document.getElementById("demo").childNodes[0].nodeValue;
    Copy after login
The following three examples retrieve the text of the

element and copy it to ## In the

# element:

Instance 1



<h1>我的第一张页面</h1>
<p>Hello!</p>

<script>
 document.getElementById("id02").innerHTML  = document.getElementById("id01").innerHTML;
</script>


Copy after login

Instance 2



<h1>我的第一张页面</h1>
<p>Hello!</p>

<script>
 document.getElementById("id02").innerHTML = document.getElementById("id01").firstChild.nodeValue;
</script>


Copy after login

Instance 3



<h1>我的第一张页面</h1>
<p>Hello!</p>

<script>
 document.getElementById("id02").innerHTML = document.getElementById("id01").childNodes[0].nodeValue;
</script>


Copy after login
InnerHTML

We use innerHTML to retrieve the content of the HTML element.

The DOM root node

has two special properties that allow access to the complete document:

document.body - the body of the document

document.documentElement - the complete document


Instance



<p>Hello World!</p>
<div>
<p>DOM 很有用!</p>
<p>本例演示 <b>document.body</b> 属性。</p>
</div>

<script>
 alert(document.body.innerHTML);
</script>


Copy after login

JavaScript HTML DOM navigation (summary sharing)Instance



<p>Hello World!</p>
<div>
<p>DOM 很有用!</p>
<p>本例演示 <b>document.documentElement</b> 属性。</p>
</div>

<script>
alert(document.documentElement.innerHTML);
</script>


Copy after login

JavaScript HTML DOM navigation (summary sharing)
JavaScript HTML DOM navigation (summary sharing)nodeName Attribute

nodeName

Attribute specifies the name of the node.

nodeName is read-only
  • The nodeName of the element node is equivalent to the label name
  • The nodeName of the attribute node is the name of the attribute
  • Text node nodeName
  • always
  • #text nodeName of document node
  • always
  • #documentInstance:
    <h1>我的第一张网页</h1>
    <p>Hello!</p>
    
    <script>
    document.getElementById("id02").innerHTML  = document.getElementById("id01").nodeName;
    </script>
    Copy after login
  • Return H1

Notes
: nodeName always contains the uppercase
tag name of the HTML element. nodeValue attribute

The nodeValue attribute specifies the value of the node.

The nodeValue of the element node is undefined
  • The nodeValue of the text node is the text text
  • The nodeValue of the attribute node is the attribute value
  • nodeType attribute

nodeType attribute returns the type of node. **nodeType is read-only.

Instance

<h1>我的第一张网页</h1>
<p>Hello!</p>

<script>
document.getElementById("id02").innerHTML  = document.getElementById("id01").nodeType;
</script>
Copy after login
Return 1

The most important nodeType attribute is:

JavaScript HTML DOM navigation (summary sharing)

Type 2 is deprecated in the HTML DOM. Not deprecated in XML DOM.

【Related recommendations: javascript video tutorial, web front-end

The above is the detailed content of JavaScript HTML DOM navigation (summary sharing). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template