今天在學習中遇到了在ie中設置class屬性值的bug,現寫出來與大家分享,一起共勉,如有錯漏望不吝指正。
如:
elm.setAttribute('class', 'className');
在IE6/7中樣式“className”并沒有起作用,雖然使用elm.getAttribute('class')是可以獲取到“className”。
后來上網查了一些資料,發現了在ie中其實還有一部分屬性也會有這樣的問題,像for屬性
<label>姓名:</label>
<input type="checkbox" id="name"/>
<script>
??? var lab = document.getElementsByTagName('label')[0];
??? lab.setAttribute('for', 'name');
</script>
理論上如果lab設置了for屬性,點擊label時會自動將對應的checkbox選中。但在IE6/7點擊并沒有選中相應的checkbox。
而且類似的情況還發生在 cellspacing/cellpadding 等屬性上,現列出來,大家可以認一下,以后做的時候可以注意一下:
class
for
cellspacing
cellpadding
tabindex
readonly
maxlength
rowspan
colspan
usemap
frameborder
contenteditable
所以,當在寫一個通用的跨瀏覽器的設置元素屬性的接口方法時,我們就需要考慮注意以上屬性在IE6/7中的特殊性。
網上就有位朋友寫了如下方法以解決上面的問題:
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'
??? },
????
??? 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));??????
??????? }
??? }
})();
首先,標準瀏覽器直接使用原始屬性名;其次,IE6/7非以上列舉的屬性仍然用原始屬性名;最后這些特殊屬性(與JS關鍵字同名如for,class)使用fixAttr。
到了這里,我們就不用考慮className/htmlFor了,直接使用class/for即可。
dom.setAttr(el, 'class', 'red');
dom.getAttr(el, 'class');
dom.setAttr(el, 'for', 'userName');
dom.getAttr(el, 'for');
如:
elm.setAttribute('class', 'className');
在IE6/7中樣式“className”并沒有起作用,雖然使用elm.getAttribute('class')是可以獲取到“className”。
后來上網查了一些資料,發現了在ie中其實還有一部分屬性也會有這樣的問題,像for屬性
<label>姓名:</label>
<input type="checkbox" id="name"/>
<script>
??? var lab = document.getElementsByTagName('label')[0];
??? lab.setAttribute('for', 'name');
</script>
理論上如果lab設置了for屬性,點擊label時會自動將對應的checkbox選中。但在IE6/7點擊并沒有選中相應的checkbox。
而且類似的情況還發生在 cellspacing/cellpadding 等屬性上,現列出來,大家可以認一下,以后做的時候可以注意一下:
class
for
cellspacing
cellpadding
tabindex
readonly
maxlength
rowspan
colspan
usemap
frameborder
contenteditable
所以,當在寫一個通用的跨瀏覽器的設置元素屬性的接口方法時,我們就需要考慮注意以上屬性在IE6/7中的特殊性。
網上就有位朋友寫了如下方法以解決上面的問題:
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'
??? },
????
??? 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

??????? }
??? }
})();
首先,標準瀏覽器直接使用原始屬性名;其次,IE6/7非以上列舉的屬性仍然用原始屬性名;最后這些特殊屬性(與JS關鍵字同名如for,class)使用fixAttr。
到了這里,我們就不用考慮className/htmlFor了,直接使用class/for即可。
dom.setAttr(el, 'class', 'red');
dom.getAttr(el, 'class');
dom.setAttr(el, 'for', 'userName');
dom.getAttr(el, 'for');
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
