Home >Web Front-end >JS Tutorial >DOM node operation usage in JavaScript (source code)
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.
1. document.createElement() //创建元素节点 2. document.createTextNode() //创建文本节点 3. document.createAttribute() //创建属性节点 4. cloneNode()使用方法:被克隆的节点对象.cloneNode(value); value 值为false(默认)或true,false表示只克隆节点及其属性,true表示克隆节点及其属性以及其后代。
例:<body>你好</body> 1. nodeValue用法 //一般用于文本节点
①document.body.getAttributeNode("属性名") ②document.body.attributes[0] //获取body中的第一个属性节点Text nodes use nodeName to return #text3. nodeType usage (Commonly used) node types:
元素节点 1 属性节点 2 文本节点 3 注释 8 文档 9 (即document.nodeType返回9)Attribute operation
⑴对象.setAttribute('属性名','属性值'); ⑵对象.className = '属性名'; ⑶var attr = document.createAttribute('属性名'); attr.nodeValue = '属性值'; 对象.setAttributeNode(attr);2. Delete attributes
⑴对象.removeAttribute('属性名'); ⑵var attr = 对象.getAttributeNode('属性名'); 对象.removeAttributeNode(attr); 附:getAttribute('属性名')返回属性值。 ⑶针对于input标签中的checkbox可使用: 对象.checked = false 使其不被选中; 附:设置对象.checked = value时,应直接将value的值 设为true或false,如果将value设置为一个字符串, 会将该字符串转化为boolean类型再赋值,会消耗性能。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!