Detailed explanation of sample code of javascript DOM
This article mainly introduces the detailed explanation of javascript DOM and relevant information about example codes. Friends in need can refer to
javascript DOM Summary
I have always thought that DOM (DocumentObjectModel) is the simplest part of JS. It is undeniable that it is indeed very simple, because the thinking mode of DOM is a bit fixed, and you only need to simply remember some fixed methods, so DOM can be said to be the starting point for all js (here refers to client js).
Recently, when I was doing some useful DOM exercises, I found that my DOM knowledge was very fragmented (I always thought I had a good grasp of it). Many friends may think that DOM is just a matter of calling Call a few methods, or I use jQuery directly, without having to consider the details of the DOM at all. Yes, that’s right. jQuery’s encapsulation of DOM is unprecedented, but just like growing up, you must be able to walk before you can run, so I think you must have a more detailed understanding of DOM, so I summarized the following about DOM Some usage methods.
In the W3C summary of DOM specifications, there are some that are very commonly used and some that are not very useful. Here we mainly discuss some commonly used DOM operations (related to DOM The basic concepts of will not be introduced here):
Node level
The so-called node level refers to the html document# There are nodes (such as tags) with their own characteristics, data, and methods in ##, and the relationship between nodes constitutes a hierarchy (in fact, it can be imagined as a tree structure). A NODE interface is defined in the W3C's DOM level 1 specification. This interface has some methods that are very useful:
Node.ELEMENT_NODE;(Element node)
Node.TEXT_NODE;(Text node)
Node.DOCUMENT_NODE (document node)
Each node has its own node type flag, which is the NodeType attribute, for example, the nodeType of the element node == ' 1';The nodeType of the text node == '3';The nodeType of the document node == '9';1. Document node
The document node is in a document It is represented by the document object, and its basic characteristics are as follows:console.log(document.nodeType); // 9 console.log(document.nodeName); // #document console.log(document.nodeValue); // null console.log(document.parentNode); // null(它已经是最顶层的节点,树的根节点)
1. By id: document.getElementById("xxx"); Generally, the page ids will be different. If there are multiple identical ids, the first element with that id will be fetched. 2. Through tagName: document.getElementsByTagName("xxx"); Return the collection of elements with the tag name "xxx"! 3. Through name: document.getElementByName(); It is very simple to understand the document object, and the compatibility is also relatively advanced.
2. Element node
The element node provides access to the element tag name, sub-nodes and attributes. Its basic characteristics are as follows:var ele = document.getElementById("element"); //通过document取到一个标签对象 console.log(ele.nodeType); // 1 console.log(ele.nodeName); // 返回元素的标签名 console.log(ele.nodeValue); //永远返回null!
tip one (html element):
<p id="myp" class="bd" title="Body text" lang="en" dir="ltr"></p> var p = document.getElementById("p"); 1. console.log(p.id); // "myp" 2. console.log(p.className); // "bd" 3. console.log(p.title); // "Body text" 4. console.log(p.lang); // "en" 5. console.log(p.dir); // "ltr"
1.p.getAttribute("id"); // "myp" 2.p.setAttribuye("id","yourp"); // id已变动 3.p.removeAttribute("id"); //id已删除
<ul id="myList"> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul> var mL = document.getElementById("myList"); //很明显mL对象有三个元素节点,我们也知道用childNodes属性去找到节点数,然而陷阱随之而来 console.log(mL.childNodes); // 7 //?!怎么会有7个? //原因在于childNodes中不仅包含了元素节点,还有4个文本节点。因此需要过滤 for(var i=0,len=ml.childNodes.length;i<len;i++){ if(ml.childNodes[i].nodeType == "1"){ // 利用元素节点的特性 // .... } }<br>//最好的方法可以这么做<br>//如果元素对象内部标签名是一样的<br>var items = ml.getElementsByTagName("li"); //能得到三个li节点对象
3. Text node
Text nodes contain plain text content that can be interpreted literally. Plain text can contain escaped HTML characters, but cannot contain HTML codes. The basic characteristics of text nodes are as follows:<p id="myp">123</p> var myp = document.getElementById("myp") //取到元素节点 var tx = myp.childNodes[0] //前面也提过childNodes的特性,这次取到了文本节点 console.log(tx.nodeType) // 3 console.log(tx.nodeName) // 所有文本节点的nodeName都是"#text"; console.log(tx.nodeValue) // 123(节点包含的文本),注意元素节点是不能取到它包含的文本节点的文本的 //所以其父节点显然是个元素节点.
var a = document.createElement("p"); var b = document.createTextNode("123"); a.appendChild(b); document.body.appendChild(a);
123
will appear at the end of the bodyPersonally, I think DOM is definitely the entry point for learning js, but it also takes a long time to polish. I have read the DOM no less than three times, it is only the DOM level 1 specification, and there is something new every time. If you learn DOM, you must pay attention to some pitfalls and spend more time thinking about them.
The above is the detailed content of Detailed explanation of sample code of 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

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

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
