特性檢測最經典的應用莫過於萬用的 addEvent 函式:
function addEvent(element, type, handler) {
if (element.attachEvent) { // IE8 及以下
element.attachEvent('on' + type, handler);
} else { // W3C 標準瀏覽器
element.addEventListener(type, handler, false);
}
}
它透過檢測 attachEvent 是否存在,決定要走哪條路徑。這是最簡單的一種特性檢測,通常只在需要時才做即時判斷。另一種則是檢測過程較繁瑣的,會預先執行一次並把結果(通常是 Boolean)存進變數,後續直接取用。
本文逐一說明 jQuery 1.4 新增的七個特性標識,順道整理幾個瀏覽器相容性的細節。
決定「沒有設定 value 的 checkbox,是否會有預設值 "on"」。
<input id="checkOn" type="checkbox" />
<script>alert(document.getElementById('checkOn').value);</script>
測試結果:IE6/7/8/9、Firefox 3.6、Safari 5 都回傳 on,只有 Chrome 7 回傳空字串。
由於不支援 checkOn 的瀏覽器都不存在 property 與 attribute 混用的問題,jQuery 的判斷很直接:
// 不支援 checkOn 的瀏覽器需明確使用 getAttribute
return support.checkOn
? element.value
: (element.getAttribute('value') === null ? 'on' : element.value);
決定「用 appendChild 為空的 select 添加 option 後,該 option 是否會預設被選中」。
<select id="optSelected"></select>
<script>
var select = document.getElementById('optSelected'),
option = document.createElement('option');
select.appendChild(option);
alert(option.selected);
</script>
測試結果:Firefox 3.6 與 Chrome 7 為 true,IE 系列與 Safari 5 為 false。
這個 bug 的具體描述是:部分瀏覽器在取得 option 的 selected 屬性時,會錯誤地回傳 false。解法是在讀取 selected 之前,先讀一次父層 select 的 selectedIndex,強迫瀏覽器把狀態算出來。注意 option 的父元素不一定是 select,也可能是 optgroup:
if (!support.optSelected) {
var parent = option.parentNode;
parent.selectedIndex; // 強迫計算
if (parent.parentNode) {
parent.parentNode.selectedIndex; // 處理 optgroup 的情況
}
}
return option.selected;
決定「select 設為 disabled 後,其 option 子元素是否也被設為 disabled」。
測試結果:只有 Safari 5 為 true,其餘皆為 false。
這個特性影響 select 值的取得。當 select 渲染為多選時,需要從中排除 disabled 的 option;但在 Safari 上,所有 option 都被標記為 disabled,導致完全取不到值。有了 optDisabled 標識就能區分:
// optDisabled 為 true 時,disabled 屬性反映 option 的真實狀態
// 否則要判斷 disabled 屬性是否為 null
var disabled = support.optDisabled
? option.disabled
: option.getAttribute('disabled') !== null;
if (!disabled) { return option.value; }
決定「對 DocumentFragment 使用 cloneNode 時,是否保留 radio/checkbox 的 checked 屬性」。
重現這個問題有兩個前提:input 必須顯式指定 name 屬性,且複製 fragment 時要連續呼叫兩次 cloneNode。
測試結果:只有 Safari 4 為 false,其餘(含 Safari 5)皆為 true。也就是說這個問題在 Safari 5 已修復。考量 Safari 的市佔與版本分布,幾乎不需要為它寫相容。
jQuery 把它用在 buildFragment 的快取機制上:簡單的 DOM 建立字串會被快取進 DocumentFragment,但遇到建立 radio 時,若 checkClone 為 false 就強制不快取。
決定「IE 下一個 block 元素擁有 hasLayout 且有 display:inline 時,是否會按 inline-block 顯示」。
IE7 以下不支援 display:inline-block,改用 display:inline 搭配觸發 hasLayout 的樣式來模擬。兩者的關鍵差異是 inline-block 可以明確設定寬高,因此:
<div id="inlineBlockNeedsLayout"
style="width: 1px; padding-left: 1px; display: inline; zoom: 1;"></div>
<script>alert(document.getElementById('inlineBlockNeedsLayout').offsetWidth);</script>
結果:IE6/7 為 2(width 1 + padding 1),IE8/9 與 Firefox 為 1,WebKit 系為 0(inline 元素 width 無效)。只有 IE7 及以下同時計算了 width 與 padding-left。
因此設定 inline-block 時針對舊 IE 要同時寫 display:inline 與 zoom:1:
if (name == 'display' && value == 'inline-block') {
if (support.inlineBlockNeedsLayout) {
element.style.display = 'inline';
element.style.zoom = 1;
} else {
element.style.display = value;
}
}
但直接這樣用是有問題的——取得 display 時難道也要同時判斷 zoom 嗎?而且 hasLayout 會引發其他副作用。所以 jQuery 只把它用在動畫:需要對 width/height 做動畫且元素為 inline 時,先設為(偽)inline-block,動畫結束後還原。
決定「IE 下一個元素擁有 hasLayout 且固定 width/height 時,是否不會被子元素撐大」。
結果:IE6 為 4(被 4px 的子元素撐大),IE7 以後及其他瀏覽器皆為 1。
jQuery 把它用在動畫,確保過程中改變 width/height 時子元素不會溢出:
決定「td 或 th 設為 display:none 時,是否還有 offsetHeight」。
結果:只有 IE8 為 21,其餘皆為 0。IE8 的 bug 是 td 設為 none 後,高度仍受其所在列影響而非 0。
這直接導致元素可見性判斷出錯。原本的寫法:
function isHidden(element) {
return element.offsetWidth == 0 || element.offsetHeight == 0;
}
對 td 失效,需改為:
function isVisible(element) {
return (element.offsetWidth == 0 && element.offsetHeight == 0)
|| (!support.reliableHiddenOffsets
&& getStyle(element, 'display') == 'none');
}
讀原始碼會發現這段多了一句 element.style.display 判斷——先用 inline style 確認元素有 display 值,才去取 computed style,藉此避免昂貴的運行時樣式查詢。這是很典型的效能取捨。
shrinkWrapBlocks、reliableHiddenOffsets 這類名字不看說明根本猜不到在測什麼。最後要補一句時代背景:本文寫於 IE9 beta 時期。以今天的標準看,IE6~IE8 已可正式退場,上述七個標識中真正仍需關注的只剩下少數與 IE8 相關的項目。但特性檢測的方法論本身完全沒有過時——今日的前端依然每天在用 'IntersectionObserver' in window、CSS.supports()、'placeholder' in document.createElement('input') 這類判斷。讀懂這段歷史,是為了更透徹地理解這套思維。