标签:
<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
标签:
原文地址:http://www.cnblogs.com/pmx-pmx/p/5823017.html