Home > Web Front-end > JS Tutorial > body text

Summary of native JavaScript operations on dom nodes

韦小宝
Release: 2018-03-09 14:10:09
Original
1895 people have browsed it

When developing JavaScript programs, the most commonly used one is the operation of dom. Today we will summarize the operations of native JavaScript on dom node. Everyone If you don’t fully understand JavaScript’s operation of DOM nodes, you can take a look.

1. Get the element node

Get a single element: document.getElementById(); document.querySelector() //Get the first element;

Get multiple elements (you can select a single element by subscript and return a collection of elements, equivalent to an array):

document.getElementsByTagName(); 
document.getElementsByName(); 
document.getElementsByClassName();// 动态,实时的 
document.querySelectorAll();// 查找速度比上面的块 //是静态的,非实时的,
Copy after login

Create Element, text

document.createElement(“p”); 
document.createNode(“hello”) //创建文件节点。
Copy after login

Insert into document

parentNode.appendChild(newNode), 
parentNode.insertBefore(newNode,positionNode);
Copy after login

Attribute operation

ele.setAttribute(name,value)//设置属性 
ele.getAttribute(name)//获取元素的属性 
ele.removeAttribute(name)//移除属性
Copy after login

Node removal

ele.remove(),parentNode.removeChild(childNode)
Copy after login

Replace child element

parentNode.replaceChild(newNode,oldNode)
Copy after login

Copy element

ele.cloneNode(boolean);// boolean为true, 深复制。
Copy after login

2. Get child elements, parent elements, sibling elements

ele.previousElementSibling;//上一个兄弟元素 
ele.parentNode;//父元素 
ele.children//子元素 
ele.nextElementSibling//下一个兄弟元素
Copy after login

More about node operations:

##Summary of DOM node operation methods in jQuery

The above is the detailed content of Summary of native JavaScript operations on dom nodes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!