Home > Web Front-end > JS Tutorial > Detailed introduction to DOM in JavaScript (code example)

Detailed introduction to DOM in JavaScript (code example)

不言
Release: 2019-03-05 15:14:01
forward
2847 people have browsed it

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);
Copy after login

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);
 }
Copy after login

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);
            }
Copy after login

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);
           }
Copy after login

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);
Copy after login

4.2: Modify node content: node object.innerHTML="new value";

eg:

ob1.innerHTML="哈哈";
Copy after login

4.3: Clear the node content:
eg:

ob1.innerHTML="";
Copy after login

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);
Copy after login

5.2: Modify the node text content (all content in the label is Modify): node object.innerText = "new value";
eg:

ob1.innerText="呵呵";
Copy after login

6.DOM operation node object properties:
6.1: Get the node object properties: node object.property name
eg:

 alert(ob2.src);
Copy after login

6.2: Modify the properties of the node object: node object.Attribute name = "value";
eg:

 ob2.src="img/img-2.jpg";
Copy after login

6.3: Delete the properties of the node object: node Object.removeAttribute("Attribute name");
eg:

ob2.attributes.removeNamedItem("title");
ob2.removeAttribute("title");
Copy after login

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";
Copy after login

7.2: Get the style of the node object: node object.style.Style name
eg:

alert(ob1.style.color);
Copy after login

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");
Copy after login

eg:

//创建节点对象
var node1=document.createElement("h1");
node1.innerHTML="你好<span>中国</span>";
Copy after login

9.1.2: Create a text object: document.createTextNode("text content");
eg:

//创建文本对象
var node1text=document.createTextNode("这是一个段落");
Copy after login

9.2: Add a node object: Node object.appendChild(child node);
eg:

//将节点对象添加body中
document.getElementById("d1").appendChild(node1);
Copy after login
//直接向一个标签中添加子节点
document.getElementById("d2").innerHTML=document.getElementById                                
("d2").innerHTML+"<h2>哈哈</h2><p>呵呵呵</p>";
Copy after login

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();
Copy after login

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);
Copy after login

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!

Related labels:
source:csdn.net
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
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template