javascript IE8 不支持 querySelectorAll

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16920365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:27:58  来源:igfitidea点击:

IE8 does not support querySelectorAll

javascriptinternet-explorer-8w3cselectors-api

提问by Roman Makhlin

I tried to use document.querySelectorAll(), but IE8 throw error, that

我尝试使用document.querySelectorAll(),但 IE8 抛出错误,即

Object doesn't support this property or method

对象不支持此属性或方法

var titleCheckBox = document.querySelectorAll("");

Here http://www.quirksmode.org/dom/w3c_core.html#t13written, that IE8 support this method. What I doing wrong?

这里写到http://www.quirksmode.org/dom/w3c_core.html#t13,说明IE8支持这种方法。我做错了什么?

回答by Spudley

Check that your page isn't in Quirks mode or Compatibility mode. You can use the F12 dev tools to confirm this. Press F12 and look in the top-right corner of the resulting window. If you see "Compatibility" or "Quirks" in the mode description, then you've found the problem.

检查您的页面是否未处于 Quirks 模式或兼容模式。您可以使用 F12 开发工具来确认这一点。按 F12 并查看结果窗口的右上角。如果您在模式描述中看到“兼容性”或“怪癖”,那么您就发现了问题。

  • Quirks mode:this is usually triggered by a missing or broken Doctype. If this is the case, make sure your page starts with the following:

    <!DOCTYPE html>
    
  • Compatibility mode (IE7 mode):This may be triggered if you're viewing the page locally (ie running it on your local machine, eg for testing, or on your local network). In this case, you are being hit by an IE config setting that you should disable. Go to the Tools menu, and pick the Comaptibility View Settings option. Untick the compatibility options, and the page should start working.

    Compat mode may also be triggered (or avoided) by an X-UA-Compatibilitymeta tag. If you're having trouble with compatibility mode, this is a good way to avoid it: Add the following line to your code:

    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    
  • 怪癖模式:这通常是由丢失或损坏的 Doctype 触发的。如果是这种情况,请确保您的页面以以下内容开头:

    <!DOCTYPE html>
    
  • 兼容模式(IE7 模式):如果您在本地查看页面(即在您的本地机器上运行它,例如用于测试,或在您的本地网络上),这可能会被触发。在这种情况下,您遇到了应该禁用的 IE 配置设置。转到“工具”菜单,然后选择“兼容性视图设置”选项。取消兼容性选项,页面应该开始工作。

    兼容模式也可以由X-UA-Compatibility元标记触发(或避免)。如果您在兼容模式下遇到问题,这是避免它的好方法:将以下行添加到您的代码中:

    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    

Either (or both) of the above could be the problem, but my guess is that the problem is compatibility mode. The compat-mode-on-intranet-sites setting is suprisingly little known, and catches a lot of people out, even some seasoned developers.

上述任何一个(或两者)都可能是问题所在,但我的猜测是兼容模式的问题。compat-mode-on-intranet-sites 设置令人惊讶地鲜为人知,并且吸引了很多人,甚至一些经验丰富的开发人员。

回答by BoltClock

IE8 only supports querySelectorAll()in standards mode. From MSDN:

IE8 仅支持querySelectorAll()标准模式。从MSDN

The Selectors API is defined as part of the Selectors APIspecification and is only available to Web pages displayed in IE8 standards mode.

Selectors API 被定义为Selectors API规范的一部分,仅适用于以 IE8 标准模式显示的网页。

Chances are your page doesn't have a proper DOCTYPE declaration; you will need to add one.

您的页面可能没有正确的 DOCTYPE 声明;你需要添加一个。