星空网 > 软件开发 > Java

JS之setAttribute和getAttribute

1.ele.getAttribute(attributeName);

 返回元素的指定属性值,如果元素没有该属性,则返回null

2.ele.setAttribute(attributeName,value);

为元素指定属性设置值,如果没有该属性,则创建该属性,并赋值

3.在IE 7以及更早版本部分属性的设置应使用另外的名称,为了兼容IE

<script>      dom=(function(){      var fixAttr={         tabindex:'tabIndex',         readonly:'readOnly',         'for':'htmlFor',         'class':'className',         maxlength:'maxLength',         cellspacing:'cellSpacing',         cellpadding:'cellPadding',         rowspan:'rowSpan',         colspan:'colSpan',         usemap:'useMap',         frameborder:'frameBorder',         contenteditable:'contentEditable'      },          //模拟设置attribute,      div=document.createElement('div');     div.setAttribute('class','t');         var supportSetAttr = div.className === 't';          return {          setAttr:function(el, name, val){            el.setAttribute(supportSetAttr ? name : (fixAttr[name] || name), val);          },          getAttr:function(el, name){            return el.getAttribute(supportSetAttr ? name : (fixAttr[name] || name));          }       }    })();        window.onload=function(){        var mydiv=document.getElementById("d1");        dom.setAttr(mydiv, 'class', 'bg');    }  </script>

在IE 7以及更早版本中,setAttribute('class','fo');能为元素添加class='fo'.

getAttribute('class');能返回fo值,但是为元素添加的类不起作用,应为IE 7及更早版本的类设置用className,而不是class

 4.

getAttribute(attributeName);不仅可以得到元素默认属性值,还能得到自定义属性值ele.attributeName或者ele['attributeName']只能得到元素默认存在的属性值

5.

var node = ele.getAttributeNode(attributeName)得到属性节点
console.log(node);//name=value的形式
console.log(node.name);console.log(node.value);
<body>  <p id="p1" customData="pmx">ppp</p>  <script>    var p = document.getElementById("p1");    var pnode = p.getAttributeNode("customData");    console.log(pnode)  </script></body>

 JS之setAttribute和getAttribute

JS之setAttribute和getAttribute

JS之setAttribute和getAttribute

 6.ownerElement返回属性节点所附属的元素

语法:

  attrObj.ownerElement

 

 


 
 

 





原标题:JS之setAttribute和getAttribute

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

跨境卖家做Shopee选品要考虑到的基本要素!:https://www.ikjzd.com/articles/129168
抢箱子 缺车子 求舱位 南美11月前物流形势严峻:https://www.ikjzd.com/articles/129169
还有这种好事!listing好评暴涨200,姐夫大发慈悲?:https://www.ikjzd.com/articles/129170
涉案货值超10亿!700万运费哪去了?30多家公司集体起诉某货代:https://www.ikjzd.com/articles/129171
欧代来袭,Amazon亚马逊卖家销售门槛越来越高!:https://www.ikjzd.com/articles/129172
欧美FBA头程物流发货会需要哪些认证?:https://www.ikjzd.com/articles/129174
中国有哪些著名的酒店品牌。:https://www.vstour.cn/a/411242.html
石象湖景区门票-石象湖景区门票优惠政策:https://www.vstour.cn/a/411243.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流