


Organize common JavaScript operations on various types of elements in DOM_Basic knowledge
Node type
nodeType
The following are some important nodeType values:
1: element element
2: Attribute attr
3: Text text
8: Comments
9: Document document
nodeName,nodeValue
Node relationship
childNodes: Each node has a childNodes attribute, which stores a NodeList object
firstChild: equivalent to childNodes[0]
lastChild: equivalent to childNodes.length-1
At the same time, other nodes in the same list can be accessed by using the previousSibling and nextSibling properties of each node in the list.
Operation Node
appendChild()
The appendChild() method is used to add a node to the end of the childNodes list. After adding a node, the relationship pointers of the new node, parent node and previous last child node of childNodes will be updated accordingly.
insertBefore()
insertBefore() This method accepts two parameters: the node to be inserted and the node used as a reference.
1 2 3 4 5 6 7 8 |
|
repaceChild()
repaceChild() accepts two parameters, the node to be inserted and the node to be replaced
1 |
|
removeChild()
Only remove nodes, not replace them.
1 |
|
cloneNode()
item 1
item 2
item 3
1 2 3 4 5 |
|
Document type
Document node has the following characteristics:
- The value of nodeType is 9;
- The value of nodeName is #document;
- The value of nodeValue is null;
- The value of parentNode is null;
- The value of ownerDocument is null;
Child node of document
1 2 3 |
|
Document information
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Calling document.getElementById("myElement"); in IE7 will return the element as shown below;
The best way is not to make the name attribute of the form field the same as the ID of other elements.
1 2 |
|
Special Collection
- document.anchors, contains all a elements with name attribute in the document;
- document.forms, contains all form elements in the document, which is the same as the result obtained by document.getElementsByTagName("form");
- document.images, contains all img elements in the document, which is the same as the result obtained by document.getElementsByTagName("img");
- document.links, contains all a elements with href attributes in the document;
Document writing
1 2 3 4 5 6 7 8 9 10 |
|
The string will not be used as the closing tag of the external script tag, so there will be no redundant content on the page.
Element type
Element node has the following characteristics:
- The value of nodeType is 1;
- The value of nodeName is the tag name of the element;
- The value of nodeValue is null;
- parentNode may be Document or Element;
To access the tag name of an element, you can use the nodeName attribute or the tagName attribute;
1 2 3 4 5 6 7 8 9 10 11 |
|
Acquire characteristics
There are three main DOM methods for operating attributes, namely getAttribute(), setAttribute(), and removeAttribute();
Note that the attribute name passed to getAttribute() is the same as the actual attribute name. It seems that if you want to get the attribute value of class, you should pass in "class" instead of "className".
1 2 |
|
Create element
New elements can be created using the document.createElement() method.
Child node of element
Before performing an operation, it is usually necessary to check the nodeType attribute first, as shown in the following example:
1 2 3 4 5 |
|
Text类型
Text节点具有以下特征:
- nodeType的值为3;
- nodeName的值为"#text";
- nodeValue的值为节点所包含的文本;
- parentNode是一个Element;
创建文本节点
可以使用document.createTextNode()创建新文本节点。
规范化文本节点
normalize()
分割文本节点
splitText()
Comment类型
comment节点具有下列特征:
- nodeType的值为8;
- nodeName的值为"#comment";
- nodeValue的值是注释的内容;
- parentNode可能是Document或Element;
- 不支持(没有)子几点;
DOM操作技术
操作表格
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
选择符API
querySelector()方法
1 2 3 4 5 6 7 8 9 10 11 |
|
querySelectorAll()方法
1 2 3 4 5 6 7 8 |
|
HTML5
与类相关的扩充
getElementsByClassName()方法:
该方法可以通过document对象及所有HTML元素调用该方法。
1 2 3 4 5 |
|
焦点管理
HTML5也添加了辅助管理DOM焦点的功能。首先就是document.activeElement属性,这个属性始终会引用DOM中当前获得了焦点的元素。
1 2 3 |
|
默认情况下,文档刚刚加载完成时,document.activeElement中保存的是document.body元素的引用。文档加载期间,docuemnt.activeElement的值为null。
另外就是新增了document.hasFocus()方法,这个方法用于确定文档是否获得了焦点。
1 2 3 |
|

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

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).

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 is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
