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 id="哈哈">哈哈</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!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools