DIV CSS 佈局教程網

javascript 中 setAttribute 的相容性問題與實用寫法分析
編輯:關於JavaScript     

setAttribute 看似單純,實作時卻是跨瀏覽器差異最集中的 API 之一。核心問題是:有些屬性用 setAttribute 設定有效,換個屬性就失效;有些在 Firefox 有效,在 IE 卻完全無作用。

四條基本原則

  1. 常規屬性建議直接用 node.XXXX 存取。
  2. 自訂屬性建議使用 node.getAttribute("XXXX")。
  3. 當目標名稱是 JavaScript 關鍵字時,建議用 getAttribute,例如 label 的 for。
  4. 當目標名稱是保留字時,例如 class,請改用 className。

方法簽名:setAttribute(string name, string value)——新增一個指定名稱與值的屬性,或把既有屬性設定為指定值。

問題一:class 屬性

setAttribute("class", value) 中的 class 指的是要改變「class」這個屬性,所以名稱必須加引號。例如:

var input = document.createElement("input");
input.setAttribute("type", "text");
input.setAttribute("name", "q");
input.setAttribute("class", bordercss);

輸出結果為 <input type="text" name="q" class="bordercss">,input 便具有 bordercss 樣式。

但這裡有個大坑:用 setAttribute("class", vName) 動態設定 class,在 Firefox 上行得通,在 IE 卻不行。因為使用 IE 內核的瀏覽器不認識 "class",必須改用 "className";而 Firefox 又不認識 "className"。常見做法是兩者並寫:

element.setAttribute("class", value);     // for Firefox
element.setAttribute("className", value); // for IE

問題二:事件與樣式屬性

再來看事件屬性:

var bar = document.getElementById("testbt");
bar.setAttribute("onclick", "javascript:alert('This is a test!');");

寫法很直觀,但 IE 不支援。注意,IE 並非不支援 setAttribute 這個函式,而是不支援用它設定某些類型的屬性——具體而言是物件屬性、集合屬性與事件屬性。也就是說,用 setAttribute 設定 style 與 onclick 在 IE 中行不通。

為達到跨瀏覽器相容,這類屬性應該改用點語法直接賦值:

document.getElementById("testbt").className = "bordercss";
document.getElementById("testbt").style.cssText = "color: #00f;";
document.getElementById("testbt").style.color = "#00f";
document.getElementById("testbt").onclick = function () {
    alert("This is a test!");
};

完整對照實驗

以下範例把各種寫法放在一起比對,可以清楚看到差異:

<div id="idHeader" class="class-header" title="kingwell" status="1"></div>
<label id="forUserName" for="userName" title="kingwell" status="1"></label>

<script type="text/javascript">
var el = document.getElementById("idHeader");

alert(el.getAttribute("id")); // 全部 -> idHeader
alert(el.id);                 // 全部 -> idHeader

alert(el.getAttribute("class"));
// IE6, IE7 -> null;IE8, IE9, Firefox -> class-header

alert(el.class);
// IE6, IE7, IE8 -> 報錯;IE9, Firefox -> undefined

alert(el.getAttribute("className"));
// IE6, IE7 -> class-header;IE8, IE9, Firefox -> undefined

alert(el.className);          // 全部 -> class-header

var elfor = document.getElementById("forUserName");

alert(elfor.getAttribute("for"));
// IE6, IE7 -> undefined;IE8, IE9, Firefox -> userName

alert(elfor.for);             // IE6, IE7 報錯;其他 -> undefined

alert(elfor.title);           // 全部 -> kingwell

alert(elfor.status);
// IE6-8 -> 1;IE9, Firefox -> undefined

alert(elfor.getAttribute("status")); // 全部 -> 1
</script>

從這張對照表能學到什麼

1. getAttribute 是最可靠的讀取方式。注意 status 這個自訂屬性:用 el.status 讀取時只有舊版 IE 有值,而 getAttribute("status") 在所有瀏覽器都正確回傳。自訂屬性一律走 getAttribute,是最省心的規則。

2. for 是 JavaScript 的保留字。因此 el.for 在部分瀏覽器會直接語法錯誤,必須用 getAttribute("for") 或 htmlFor。

3. class 的情況最混亂。className 是標準寫法,且是唯一在所有瀏覽器都回傳正確值的屬性存取方式。setAttribute("class", ...) 則是舊 IE 的地雷。

4. 這些差異今日多已無關。本文描述的相容性問題源自 IE6 到 IE9 的時代。現代瀏覽器已完整支援標準 DOM,setAttribute("class", ...) 與 el.classList 都能正常運作。這份分析的價值在於理解「為什麼 classList 的設計是這樣」,以及維護老系統時該往哪裡找問題。

更多 JavaScript 相關內容可參考本站的其他專題整理,包括常用函式技巧、物件導向入門、JSON 操作、切換特效、查找與遍歷演算法、動畫特效,以及錯誤與除錯技巧等主題。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved