属性:
1 .nodename
节点名称,相当于tagname.属性节点返回属性名,文本节点返回#text。nodename,是只读的。
2 .nodetype
值:1,元素节点;2,属性节点;3,文本节点。nodetype是只读的。
3 .nodevalue
返回一个字符串,指示这个节点的值。元素节点返回null,属性节点返回属性值,文本节点返回文本。nodevalue可读可写,这是对元素节点不能写。一般只用于设置文本节点的值。
4 .childnodes
返回子节点数组。文本和属性节点的childnodes永远是null。可以用haschildnodes()来判断是否有子节点。只读属性,要删除添加节点可不能用操作childnodes数组的办法呃。
5 .firstchild
返回第一个子节点。文本和属性节点没有子节点,会返回一个空数组,这是针对这二位的特殊待遇。对于元素节点,若是没有子节点会返回null.有一个等价式:firstchild=childnodes[0].
6 .lastchild
返回最后一个子节点。返回值同firstchild,三方待遇参考上面。有一个等价式:lastchide=childnodes[childnodes.length-1].
7 .nextsibling()
返回节点的下一个兄弟节点。如果没有下一个兄弟节点的话,返回null。只读属性,不可以更改应用。
8 .previoussibling()
返回节点的上一个兄弟节点。同上。
9 .parentnode()
返回节点的父节点。document.parentnode()返回null,其他的情况下都将返回一个元素节点,因为只有元素节点拥有子节点,出了document外任何节点都拥有父节点。parentnode(),又是一个只读的家伙。
操作:
1. 创建节点
createelement('tagname');
如:var op=document.createelement('p');创建了一个
标签。
2. 创建文本节点
createtextnode('text');
如:var otext=document.createtextnode('this is a paragh!');
3. 附加子节点
appendchild(o);其中o为节点对象。
如:document.body.appendchildnode(o);在body末尾追加
document.forms[0].appendchildnode(o);在form表单末尾追加
op.appendchildnode(o);在元素内部的末尾追加,其总op为节点对象。
4. 创建文档片断
createdocumentfragment();
如:var of=document.createdocumentfragment();
5. 删除节点
removechild(op);
如:document.body.removechild(op),从body中移除op节点对象。
6. 替换节点
replacechid(newop,targetop);将目标节点targetop替换为newop
如:document.body.replaychild(opa,opb).ps:怎会这样特殊?源和目地操作数都是参数,为何调用者是document.body?记住先,别多管。——被替换的必须是body的子节点,可以用其他element替代document.body,前提一样,被替换的要是这个element的子节点。
7. 插入节点
insertbefor(newop,targetop);
insertafter(newop,targetop);
8. 设置或得到属性节点
setattribute('key','value');
getattribute('key','value')
9.复制节点。
clonenode(true/false)