Home Web Front-end JS Tutorial Detailed explanation of DOM operations in JavaScript

Detailed explanation of DOM operations in JavaScript

Aug 13, 2017 am 10:33 AM
javascript js Detailed explanation

1. What is DOM

JavaScript consists of three parts: ECMAScript, DOM and BOM. DOM represents the method and interface for describing web page content, that is, the document Object Model (Document Object Model). On a web page, the objects that organize the page (or document) are organized in a tree structure. The standard model used to represent the objects in the document is called DOM, and the tree structure is what we often call the DOM tree.

The introduction of DOM in Wikipedia is more accurate: DOM is a cross-platform and language-independent application programming interface that processes current HTML, XHTML or XML documents as a tree structure, in which each node is an object, each node represents a part of the document.

2. DOM node

1. Type of node

The DOM tree is composed of many different types of nodes, and these node types have a nodeType value. We can use the nodeType value to determine whether what we obtain or want is a node of the corresponding type.

Node type Element node (label node) Attribute node Text node (space, newline, Text) Comment node document node
nodeType value 1 2 3 8 9

In addition to judging the type by the nodeType value, we can also use nodeName to view the node names of element nodes and attribute nodes, and use nadeValue to view the attribute values ​​of attribute nodes.

2. Get the element node


document.getElementById(id);
document.getElementByTagName(tagname);
document.getElementByClassName(classname);
Copy after login

Usually you want to do this in JavaScript When updating or using element nodes in HML, they must be obtained before calling. Then we obtain it by setting the Id attribute or Class attribute of the element, or by using the tag name. But be aware that the getElementById() method does not work in XML. In XML documents, searches must be performed by having the id attribute, and this type must be declared in the XML DTD.

After obtaining the node we want, we can also operate other nodes through the relationship of the nodes. Here we take the p element as the initial element node as an example.

 

2.1 Child node


p.children();
p.childNodes();
Copy after login

Get The child nodes under p, while p.children() can only get the child nodes of the element node, while p.childNodes() will get all types of child nodes, which means that it will parse blank text nodes.

 2.2 Parent node


p.parentNode();
p.offsetParent();
Copy after login

Obtain the parent node of p. p.offsetParent() obtains the ancestor node with positioning attributes, that is, starting from the parent node. If the parent node is not positioned attribute, then look for the parent node of the parent node until a node with a positioning attribute is found.

 2.3 Brother Node


//下一个兄弟节点 p.nextElementSibling();
p.nextSibling();//上一个兄弟节点 p.previousElementSibling();
p.previousSibling();
Copy after login

Whether there is an Element here is very important. Just like getting child nodes, p.nextSibling() and p.previousSibling() will parse blank nodes and get p space or enter below.

 2.4 First and last child nodes


//获取p的第一个子节点p.firstElementChild();
p.firstChild();//获取p的最后一个子节点p.lastElementChild();
p.lastElementChild();
Copy after login

The difference is the same as above.

3. Node operation

In addition to adding and deleting modification nodes in HTML, we can also operate nodes in JavaScript.

 3.1 Create node


//创建节点/创建文本节点createElement();
createTextNode();
Copy after login

Creating text nodes is generally used to add content to element nodes. It creates static text and cannot have HTML format like innerHTML, so creatTextNode() is safer, and innerText There are browser compatibility issues.

 

3.2 Add Node

The nodes we create will not be automatically added to HTML, we need to operate the created nodes.


//向尾部添加子节点 appendChild(); 
//向目标节点之前添加insertBefore(newElement,targetElement);
Copy after login

The second parameter of insertBefore() is an optional parameter. If you do not write the second parameter, it will be added to the end by default, which is equivalent to appendChild();

 

3.3 Replace


//将旧节点换为新节点replaceChild(newElement,oldElement);
Copy after login

 p.replaceChild(newElement,oldElement), Here, whether it is a new node or an old node, it must be a child node of p.

 

3.4 Delete


removeChild();
Copy after login

Note that the child nodes are deleted.

 

3.5 Clone/Copy


//深复制或浅复制cloneNode(boolean);
Copy after login

When the Boolean value of the parameter is true, it is a deep copy. That is, the node itself and all child nodes below it are copied.

When the Boolean value of the parameter is false, it is shallow copy, and only the node itself will be copied.

 

3.6 Determination

##

hasChildNode();
Copy after login

Determine whether there are child nodes and return a Boolean value.

 3.7 Properties

//获取节点属性getAttribute();//设置节点属性setAttribute();//删除节点属性removeAttribute();
Copy after login

  要注意的一点就是class属性不能通过setAttribute(); 设置。

三、DOM的优缺点

  DOM的优点主要表现在:易用性强,并且遍历简单,支持XPath,增强了易用性。

  DOM的缺点主要表现在:效率低,解析速度慢,内存占用量过高。

The above is the detailed content of Detailed explanation of DOM operations in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of obtaining administrator rights in Win11 Detailed explanation of obtaining administrator rights in Win11 Mar 08, 2024 pm 03:06 PM

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

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

Detailed explanation of division operation in Oracle SQL Detailed explanation of division operation in Oracle SQL Mar 10, 2024 am 09:51 AM

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

Detailed explanation of the role and usage of PHP modulo operator Detailed explanation of the role and usage of PHP modulo operator Mar 19, 2024 pm 04:33 PM

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Detailed explanation of the linux system call system() function Detailed explanation of the linux system call system() function Feb 22, 2024 pm 08:21 PM

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

Detailed explanation of Linux curl command Detailed explanation of Linux curl command Feb 21, 2024 pm 10:33 PM

Detailed explanation of Linux's curl command Summary: curl is a powerful command line tool used for data communication with the server. This article will introduce the basic usage of the curl command and provide actual code examples to help readers better understand and apply the command. 1. What is curl? curl is a command line tool used to send and receive various network requests. It supports multiple protocols, such as HTTP, FTP, TELNET, etc., and provides rich functions, such as file upload, file download, data transmission, proxy

The relationship between js and vue The relationship between js and vue Mar 11, 2024 pm 05:21 PM

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

Learn more about Promise.resolve() Learn more about Promise.resolve() Feb 18, 2024 pm 07:13 PM

Detailed explanation of Promise.resolve() requires specific code examples. Promise is a mechanism in JavaScript for handling asynchronous operations. In actual development, it is often necessary to handle some asynchronous tasks that need to be executed in sequence, and the Promise.resolve() method is used to return a Promise object that has been fulfilled. Promise.resolve() is a static method of the Promise class, which accepts a

See all articles