一、访问节点
在W3C的DOM中,每个元素,容器,文本块都可以看成一个节点,节点是DOM 基本组成元素。
DOM 中定义的HTML节点类型:
1 元素 h
2 属性 color=“red”;
3 文本 Hello
8 注释 /* */
9 文档 <html>
10 文档类型 <!doctype>
二、
节点的基本操作
<span style="font-size:18px;">var div=document.createElement("div");//创建元素标记
document.createComment("zhushi "); 创建注释节点
document.createTextNode("wenbenjiedian "); 创建文本节点
document.createAttribute("shuxing"); 创建属性</span><span style="font-size:18px;">其他:</span><span style="font-size:18px;">document.createStyleSheet();
document.createEventObject();
document.createElementNS();
</span>三、插入和添加节点
<span style="font-size:18px;">var ull=document.getElementById("nnew").childNodes.item("1");
ull.appendChild(nn);
ull.insertBefore(nn,ull.lastChild);
ull.insertBefore(nn,ull.firstChild);</span><span style="font-size:18px;">四、复制、删除、替换节点</span><span style="font-size:18px;">cloneNode(bool);//复制节点,bool是否复制子节点</span><span style="font-size:18px;">removeNode(childName);//删除由childname指定的节点</span><span style="font-size:18px;">replaveChild(newchild,oldchild);用新节点替换旧节点</span><span style="font-size:18px;"> for(var j=0;j<5;j++){
c=nn.cloneNode(true);
ull.appendChild(c);
}
</span>五、修改节点
<span style="font-size:18px;"> var nn=document.createElement("li");
var te=document.createTextNode("woaini"+i);
nn.appendChild(te);
console.log(nn.outerHTML+i);
i--;
var ull=document.getElementById("nnew").childNodes.item("1");
nn.appendData("new string");
nn.deleteData(offset,count);//delete from offset use count
nn.insertData(offset,string);//insert from offset by string
nn.replaceData(offset,count,string);
nn.splitText(offset);
nn.substringData(offset,count);</span><span style="font-size:18px;">六、Node对象方法</span><span style="font-size:18px;"></span><pre name="code" class="html"> nn.hasChildNodes;//boolean
nn.selectNodes(pattern);//accept type
nn.selectSingleNode(pattern);//only one七、树节点
<span style="font-size:18px;"></span><pre name="code" class="html"> var eee= nn.nextElementSibling;
var ee= nn.nextSibling;
var e= nn.nextNode;
var a=nn.previousElementSibling.previousSibling;练习:var i=2;
do{
var topc=document.getElementById("top");
console.log(topc.nodeName+"——》顶部标签名字");
console.log(topc.childElementCount.toFixed(2)+"-->顶部标签子元素数量");
for(var j=0;j<topc.childNodes.length;j++){
console.log(topc.childNodes.item(i).nodeType);
}
console.log(topc.childNodes[1]==topc.childNodes.item("1"));//+"证明了childNodes[0]与childNodes.item('1')用法相同--名字为"
console.log(topc.childNodes[0].nodeName);
console.log(topc.firstElementChild.nodeName);
console.log(topc.firstElementChild.nextElementSibling.nodeName);
console.log(topc.firstElementChild.nextElementSibling.childNodes[0].valueOf());
console.log(topc.firstElementChild.nextElementSibling.childNodes[0].toString());
console.log(topc.firstElementChild.nextElementSibling.innerHTML);
console.log(topc.firstElementChild.nextElementSibling.querySelectorAll("li").length);
console.log(topc.lastElementChild.previousElementSibling.nodeName);
console.log(topc.lastElementChild.previousElementSibling.childNodes[0].appendData("zhang"));
console.log(topc.lastElementChild.previousElementSibling.childNodes[0].valueOf());
console.log(topc.childNodes.item("1").parentNode.nodeName.toUpperCase()=="DIV");
i--;
}while(i>1)
1394




被折叠的 条评论
为什么被折叠?



