属性处理方法总结:
注意:在DOM核心1,2,3,Attr继承Node接口,但在DOM4,Attr不再继承Node接口.
判断是否存在属性和返回属性集合:
Element.hasAttributes()返回Boolean值,指示当前元素是否有任何属性
Element.attributes 返回一个NamedNodeMap类型对象,集合中的元素会实时更新。
没有命名空间,最常使用的方法:
Element.setAttribute();
Element.getAttribute();
Element.removeAttribute();
Element.hasAttribute();
根据属性名获取属性,如果属性名不存在会返回空字符串,因此需要hasAtribute()用来判断是否存在这个属性.
有命名空间(DOM Level 2)
Element.setAttributeNS();
Element.getAttributeNS();
Element.removeAttributeNS();
Element.hasAttributeNS();
直接处理Attr节点方法(DOM Level 1):
Element.setAttributeNode();
Element.getAttributeNode();
Element.removeAttributeNode();
有命名空间Attr节点处理方法:
Element.setAttributeNodeNS();
Element.getAttributeNodeNS();
之所以没有删除方法是因为删除不需要name,localName,namespaceURI,与直接使用removeAttributeNode一样.
NamedNodeMap表示的是Attr对象的一个集合,NamedNodeMap内的对象没有任何特别的顺序,可以通过类似数组索引一样去访问。
length只读的属性
getNamedItem();//根据给定的名字返回一个Attr
setNamedItem();
removeNamedItem();
item();
getNamedItemNS();
setNamedItemNS();
removeNamedItemNS();
创建属性:
document.createAttribute();
document.createAttributeNS();