Detailed introduction to DOM in JavaScript (code example)
The content of this article is a detailed introduction (code example) about DOM in JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.
1. DOM: Document Object (document) model. Think of the entire HTML page as an upside-down tree. HTML is the root node of the tree, and head and body are the child nodes of the tree. The DOM model requires that each pair of tags in html be regarded as a node object for operation
2. The role of DOM:
JavaScript can change the content of the page All HTML elements
JavaScript can change all HTML attributes in the page
JavaScript can change all CSS styles in the page
JavaScript can react to all events in the page
3.DOM search for element node objects in the page:
3.1: Search for an element node object in the page by id
eg:
var ob1=document.getElementById("d1"); //将节点对象中内容输出 alert(ob1.innerHTML);
3.2: Search by tag name The collection or array of element nodes in the page
eg:
var arr1=document.getElementsByTagName("h2"); //遍历节点对象集合,输出每个对象的内容 for(var i=0;i<arr1.length;i++){ alert(arr1[i].innerHTML); }
3.3: Search the collection or array of element nodes in the page through the class name
eg:
var arr2=document.getElementsByClassName("c1"); //遍历节点对象集合,输出每个对象的内容 for(var i=0;i<arr2.length;i++){ alert(arr2[i].innerHTML); }
3.4: Through the name attribute Find the element node collection or array
eg:
var arr3=document.getElementsByName("hobby"); //遍历节点对象集合,输出每个对象的value属性值 for(var i=0;i<arr3.length;i++){ alert(arr3[i].value); }
4. The content of the DOM operation node object (text content in the label, sub-label, sub-label text...):
4.1 : Get node content: node object.innerHTML
eg:
alert(ob1.innerHTML);
4.2: Modify node content: node object.innerHTML="new value";
eg:
ob1.innerHTML="哈哈";
4.3: Clear the node content:
eg:
ob1.innerHTML="";
5.DOM operates the text content of the node object (text in the label and text in the sub-label...):
5.1: Obtain the node text content (the text in the label and the text in the sub-label): node object.innerText
eg:
alert(ob1.innerText);
5.2: Modify the node text content (all content in the label is Modify): node object.innerText = "new value";
eg:
ob1.innerText="呵呵";
6.DOM operation node object properties:
6.1: Get the node object properties: node object.property name
eg:
alert(ob2.src);
6.2: Modify the properties of the node object: node object.Attribute name = "value";
eg:
ob2.src="img/img-2.jpg";
6.3: Delete the properties of the node object: node Object.removeAttribute("Attribute name");
eg:
ob2.attributes.removeNamedItem("title"); ob2.removeAttribute("title");
7.DOM operation node object style:
7.1: Set the style of the node object: Node object.style.Style name=" Style value";
eg:
ob1.style.color="red"; ob1.style.backgroundColor="blue";
7.2: Get the style of the node object: node object.style.Style name
eg:
alert(ob1.style.color);
8.Event
8.1: onload: Page loading event.
8.2: onclick: Mouse click event.
8.3: onchange: change event.
8.4: onblur: Cursor leaves event.
8.5: onfocus: Get cursor event.
8.6: onmouseover: mouse passing event.
8.7: onmouseout: mouse leaves event.
9.DOM operation node object
9.1: Create node object:
9.1.1: Create label node object: document.createElement("label name");
eg:
//创建节点对象 var node1=document.createElement("p");
eg:
//创建节点对象 var node1=document.createElement("h1"); node1.innerHTML="你好<span>中国</span>";
9.1.2: Create a text object: document.createTextNode("text content");
eg:
//创建文本对象 var node1text=document.createTextNode("这是一个段落");
9.2: Add a node object: Node object.appendChild(child node);
eg:
//将节点对象添加body中 document.getElementById("d1").appendChild(node1);
//直接向一个标签中添加子节点 document.getElementById("d2").innerHTML=document.getElementById ("d2").innerHTML+"<h2>哈哈</h2><p>呵呵呵</p>";
9.3: Delete node object: parent node object.removeChild(child node object);
eg:
//获得父节点对象 var parentNode=document.getElementById("d1") //获得要删除的子节点对象 var childNode=document.getElementsByTagName("p")[0]; //删除子节点对象 //parentNode.removeChild(childNode); //删除当前节点对象,只有谷歌,火狐 childNode.remove();
9.4: Copy the node object: node object.cloneNode(true);
eg:
//获得要复制的节点对象 var childNode=document.getElementsByTagName("p")[0]; //复制节点对象,true表示复制节点的同时将内容复制,false反之 var copyNode=childNode.cloneNode(true); //将复制的节点添加到body中 document.getElementById("d1").appendChild(copyNode);
9.5: Replace the child node in the element: parent node object.replaceChild(newnode,oldnode);
The above is the detailed content of Detailed introduction to DOM in JavaScript (code example). 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).

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
