你的位置:首页 > 软件开发 > Java > JS之setAttribute和getAttribute

JS之setAttribute和getAttribute

发布时间:2016-09-13 00:00:09
1.ele.getAttribute(attributeName); 返回元素的指定属性值,如果元素没有该属性,则返回null2.ele.setAttribute(attributeName,value);为元素指定属性设置值,如果没有该属性,则创建该属性,并赋值3.在IE ...

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>

 

原标题:JS之setAttribute和getAttribute

关键词:JS

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