setAttribute bug By 司徒正美
請在IE6與IE7下瀏覽,當然IE8也可以,我已讓IE8處在IE7的兼容模式下運作。兼容模式連bugs也兼容了……
解決辦法有兩個,如用**innerHTML**,覺得**innerHTML**真是一個偉大的發明,連火狐與W3C那幫死對頭也不得不屈服。
setAttribute bug By 司徒正美
請在IE6與IE7下瀏覽
另一個利用IE強大的**createElement**特徵,它能在創建元素的同時,連屬性也一起創建。
setAttribute bug By 司徒正美
請在IE6與IE7下瀏覽
但name只是冰山一角,**setAttribute**在設置屬性時,有許多屬性在IE下與標準瀏覽器的命名是不一樣的,看一下jQuery,發現它也是不全的。許多地雷埋在這裡,總有一個你遲早會中的。下面是一個詳盡的參照表:左邊為標準瀏覽器的,右邊是IE的。
var IEfix = {
acceptcharset: "acceptCharset",
accesskey: "accessKey",
allowtransparency: "allowTransparency",
bgcolor: "bgColor",
cellpadding: "cellPadding",
cellspacing: "cellSpacing",
"class": "className",
colspan: "colSpan",
checked: "defaultChecked",
selected: "defaultSelected",
"for": "htmlFor" ,
frameborder: "frameBorder",
hspace: "hSpace",
longdesc: "longDesc",
maxlength: "maxLength",
marginwidth: "marginWidth",
marginheight: "marginHeight",
noresize: "noResize",
noshade: "noShade",
readonly: "readOnly",
rowspan: "rowSpan",
tabindex: "tabIndex",
valign: "vAlign",
vspace: "vSpace"
}
IE不能用**setAttribute**為dom元素設置onXXX屬性,換言之,不能用**setAttribute**設置事件。
setAttribute bug By 司徒正美
用setAttribute設置事件
在IE下文本域獲得焦點後並沒有彈出預期的alert!
IE要直接賦給一個函數!
var body = document.body;
var form = document.createElement("form");
form.innerHTML = ""
body.appendChild(form);
if(!+"\v1"){
form.elements.test.setAttribute("onfocus", function(){alert(this.name)});
}else{
form.elements.test.setAttribute("onfocus", "alert(this.name)");
}
setAttribute bug By 司徒正美
IE用setAttribute設置事件要直接賦函數!
在IE6與IE7中也不能用**setAttribute**設置樣式:dom.setAttribute("style","font-size:14px")
setAttribute bug By 司徒正美
IE6與IE7看不到效果!
這時要統一用dom元素的**style.csstext**屬性賦值比較安全。
setAttribute bug By 司徒正美
h3.style.setAttribute('cssText', styleData);
總結:各個瀏覽器的標準的不統一確實給我們在網站製作的過程中帶來很多的麻煩,遇到這種問題也是我們特別頭痛的事情,這時我們試著換一種思路來考慮問題,可能也會得到異曲同工之妙。最後,我個人感覺這個問題也並不能說是IE瀏覽器的問題,只能說w3c在制定標準的時候欠缺全面的考慮!畢竟很多標籤是IE瀏覽器制定的。
版權聲明:轉載時請以超鏈接形式標明文章原始出處和作者信息及本聲明\\
文章引用地址:http://www.iefans.net/ie-setattribute-bug/ 作者:iefans