HTML元素属性处理方法

属性处理方法总结:

注意:在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();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值