首页 > 文章列表 > 我如何编写一个脚本,根据其可用性使用W3C DOM或IE 4 DOM?

我如何编写一个脚本,根据其可用性使用W3C DOM或IE 4 DOM?

177 2023-09-16

如果您希望编写一个能够根据其可用性灵活地使用 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
}