DOM 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
Take a look at one of the texts below.
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.
Core DOM and HTML DOM We already know that DOM can depict HTML and XML documents. In fact, an HTML document is an XML document, but more standardized. Therefore, as part of DOM Level 1, the Core DOM specification applies to all XML documents and the HTML DOM specification extends Core DOM. Of course, HTML DOM does not apply to all XML documents, only to HTML documents. Let’s take a look at the constructors of Core DOM and HTML DOM.
Constructor relationship
Access DOM node Before validating the form or changing the image, we need to know how to access the element (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 nodeType, nodeName, nodeValue attributes. Let’s take a look at the nodeType of document
document.nodeType;//9
Copy after login
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.
var bd = document.documentElement.childNodes[1];
bd.childNodes.length;//9
Copy after login
Let’s look at the structure of the body
<body>
<p class="opener">first paragraph</p>
<p><em>second</em> paragraph</p>
<p id="closer">final</p>
<!-- and that's about it -->
</body>
Copy after login
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
.
There are 9 nodes in total.
Properties Because the first node is a blank node, the second node is the first p label.
Access the content in the tag Let’s look at the first tag p
You can access it using the textContent property. It should be noted that textContent does not exist in IE browser. Please replace it with innerText. The result will be the same.
bg.childNodes[1].textContent;// "first paragraph"
Copy after login
There is also an attribute called innerHTML. This is not a DOM specification. But all major browsers support this attribute. What it returns is HTML code.
bg.childNodes[1].innerHTML;// "first paragraph"
Copy after login
There is no html code in the first paragraph, so the result is the same as textContent (innerText in IE). Let’s take a look at the second tag containing HTML code
<body>
<p class="opener">first paragraph</p>
<p id="closer">final</p>
<!-- and that's about it -->
</body>
Copy after login
我们来看看replaceChild的使用。我们把上一个删除节点来替代第二个p
var replaced = document.body.replaceChild(removed, p);
Copy after login
和removeChild返回一样。replaced就是移除的节点。现在结果为
<body>
<p class="opener">first paragraph</p>
<p><em>second</em> paragraph</p>
<!-- and that's about it -->
</body>
Copy after login
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
How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.
Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price
Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side
WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology
With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese
Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order
JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest
JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We