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

DOM node operation usage in JavaScript (source code)

云罗郡主
Release: 2018-10-18 14:01:20
forward
2279 people have browsed it

The content this article brings to you is about the usage of DOM node operations in JavaScript (source code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

DOM node operation usage in JavaScript (source code)

How to create a node

1. document.createElement()  //创建元素节点
2. document.createTextNode() //创建文本节点
3. document.createAttribute() //创建属性节点
4. cloneNode()使用方法:被克隆的节点对象.cloneNode(value); 
value 值为false(默认)或true,false表示只克隆节点及其属性,true表示克隆节点及其属性以及其后代。
Copy after login

Some usage of nodes

例:<body>你好</body>
1. nodeValue用法  //一般用于文本节点
Copy after login

2. nodeName usage // Generally used for element nodes, the returned element names are all uppercase letters

##document. body.nodeName //BODY

How to obtain attribute nodes:

①document.body.getAttributeNode("属性名")
②document.body.attributes[0] //获取body中的第一个属性节点
Copy after login

Text nodes use nodeName to return #text

3. nodeType usage (Commonly used) node types:

元素节点   1 
    
属性节点   2  
文本节点  3   
注释      8   
 文档      9   (即document.nodeType返回9)
Copy after login

Attribute operation

⑴对象.setAttribute(&#39;属性名&#39;,&#39;属性值&#39;);
 ⑵对象.className = &#39;属性名&#39;;
 ⑶var attr = document.createAttribute(&#39;属性名&#39;);
    attr.nodeValue = &#39;属性值&#39;;
    对象.setAttributeNode(attr);
Copy after login

2. Delete attributes

    ⑴对象.removeAttribute(&#39;属性名&#39;);
    ⑵var attr = 对象.getAttributeNode(&#39;属性名&#39;);
    对象.removeAttributeNode(attr);
    附:getAttribute(&#39;属性名&#39;)返回属性值。
    ⑶针对于input标签中的checkbox可使用:
    对象.checked = false 使其不被选中;
    附:设置对象.checked = value时,应直接将value的值
    设为true或false,如果将value设置为一个字符串,
    会将该字符串转化为boolean类型再赋值,会消耗性能。
Copy after login
The above is the complete introduction, if you want to know more about

JavaScript video tutorial , please pay attention to the PHP Chinese website.

The above is the detailed content of DOM node operation usage in JavaScript (source code). 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
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!