An article on detailed operations on JavaScript DOM
This article brings you relevant knowledge about javascript, which mainly introduces related issues about the detailed operation of DOM, including what is DOM, what is DOM Tree, how to obtain DOM, etc. Let’s take a look at the content below, I hope it will be helpful to everyone.
[Related recommendations: javascript video tutorial, web front-end]
What is DOM?
Document Object Model
, abbreviated DOM, Chinese: Document Object Model
, is the standard programming interface## recommended by the W3C organization for processing extensible markup languages.
DOM Tree refers to
parsing the
HTML page through
DOM and
generating The
HTML tree
tree structure and the corresponding
access method, with the help of DOM Tree, we can directly and
easily operate on the HTML page The content of each tag, such as the following HTML code
<title>玩转dom</title> <p>我是一个dom节点</p> <p> </p><p>p p</p>
1. Get by id Syntax:
document.getElementById("id name");
<p>我是p节点</p> <script> var p = document.getElementById("p"); console.log(p); </script>
2. Obtain via tag name Syntax:
document.getElementsByTagName("tag name");
<p>我是p节点</p> <p>我也是p节点</p> <script> var p = document.getElementsByTagName("p"); console.log(p); for (let i = 0; i < p.length; i++) { console.log(p[i]); } </script>
Note: Use the getElementsByTagName() method to return a collection of objects with the specified tag name, because what we get is a collection of objects, so we want To operate the elements inside, you need to traverse. Note: The element object obtained using this method is dynamic
3. Obtain through the class name Syntax:
document.getElementsByClassName("class name");
<p>我是p节点</p> <p>我是p节点</p> <script> var p = document.getElementsByClassName("p"); console.log(p); for (let i = 0; i < p.length; i++) { console.log(p[i]); } </script>
4. Obtain [Recommendation] through HTML5 new api Grammar:
document.querySelector("详见实例");
document.querySelectorAll("详见实例");
<p>我是p节点</p> <p>梨花</p> <p>信息</p> <script> // 通过标签名获取 var p = document.querySelector("p"); // 通过类名获取,记得加点 var qname = document.querySelector(".name"); // 通过id获取,记得加# var info = document.querySelector("#info"); // 获取匹配到的所有元素,返回数组 var all = document.querySelectorAll("p"); console.log(p); console.log(qname); console.log(info); for (let i = 0; i < all.length; i++) { console.log(all[i]); } </script>
document.body;
<script> var body = document.body; console.log(body); </script>
You can see that all the contents of the body element were successfully obtained
Get html element Syntax:
document.documentElement;
<script> var html = document.documentElement; console.log(html); </script>
As you can see, the entire web page html has been obtained. OK, so far, getting the DOM has come to an end. Now let’s start learning to dynamically create and add DOM
document.createElement("元素名");
If you want to dynamically create an element
p, you can write it like this. The same is true for other things. Draw inferences from one example
var p = document.createElement("p");
node.appendChild(child);
<p> <a>百度一下</a> </p> <script> var p = document.createElement("p"); p.innerText = "我就是p" var p = document.querySelector("p"); p.appendChild(p); </script>
动态创建元素p段落标签,并写入文字“我就是p”,最后获取p元素,并将p追加为p的孩子,这种追加方式是在末尾追加,因此效果如上图所示
指定元素后追加
语法:
node.insertBefore(child, 指定元素);
实例:
<p> <a>百度一下</a> <span>我是span弟弟</span> </p> <script> var p = document.createElement("p"); p.innerText = "我就是p" var p = document.querySelector("p"); var a = document.querySelector("a"); // 在p下创建p,位置在a元素之前 p.insertBefore(p, a); </script>
这就完了?对啊,你以为呢?是不是很简单呢,简单就对了,剩下的就是要多练习了,好,进入下一环节,如何修改 DOM 呢?
修改 DOM
总结如下:
例子1:获取页面的p标签,并将内容改为 “周棋洛”
<p> </p><p></p> <script> var p = document.querySelector("p"); p.innerText = "周棋洛"; </script>
例子2:点击按钮生成百度的超链接
<p> <button>点击生成百度超链接</button> </p> <script> function createBaidu() { var p = document.querySelector("p"); var a = document.createElement("a"); a.href = "https://www.baidu.com"; a.innerText = "百度一下,你就知道"; p.append(a); } </script>
例子3:点击按钮,p标签内文字颜色变绿,手动狗头
<p> <button>点击变绿</button> </p><p>我一会就变绿</p> <script> function changeColor() { var p = document.querySelector("p"); p.style.color = "green"; } </script>
删除 DOM
node.removeChild() 方法从 DOM 中删除一个子节点,返回删除的节点
语法:
node.removeChild(child);
案例:
<p> <button>点击移除p</button> </p><p>我是p,一会就时间到了</p> <script> function removeP() { var p = document.querySelector("p"); var p = document.querySelector("p"); p.removeChild(p); } </script>
【相关推荐:javascript视频教程、web前端】
The above is the detailed content of An article on detailed operations on JavaScript DOM. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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

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

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

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

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

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service
