如何編寫一個指令碼以便根據其可用性使用 W3C DOM 或 IE 4 DOM?


如果你想編寫一個指令碼,該指令碼可以靈活使用 W3C DOM 或 IE 4 DOM(取決於它們的可用性),那麼你可以使用一個功能測試方法,該方法首先檢查方法或屬性的存在,以確定瀏覽器是否具備你期望的功能。

以下是顯示相同內容的程式碼片段 −

if (document.getElementById) {
   // If the W3C method exists, use it
}

else if (document.all) {
   // If the all[] array exists, use it
}

else {
   // Otherwise use the legacy DOM
}

更新於: 2020 年 6 月 23 日

85 次瀏覽

啟動您的 事業

完成課程以獲得認證

開始學習
廣告