javascript IE9 数组不支持 indexOf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7792195/
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
IE9 array does not support indexOf
提问by tanyehzheng
Based on http://ie.microsoft.com/testdrive/HTML5/ECMAScript5Array/Default.html, I thought IE9 supports indexOf in array but the following breaks. Any idea why?
基于http://ie.microsoft.com/testdrive/HTML5/ECMAScript5Array/Default.html,我认为 IE9 支持数组中的 indexOf 但以下中断。知道为什么吗?
<script type="text/javascript">
var a = [59, 20, 75, 22, 20, 11, 63, 29, 15, 77];
var result = a.indexOf(32);//
document.write(result);
</script>
Error message as below:
错误信息如下:
SCRIPT438: Object doesn't support property or method 'indexOf'
test.php, line 9 character 1
test.php,第 9 行字符 1
回答by Aux
Are you sure your page is running in IE9 mode? Check in dev tools (F12). If you have old DOCTYPE you might be seeing your page in IE8/7 mode, so indexOf is not supported. If you are running in IE9 mode then it works just fine.
您确定您的页面在 IE9 模式下运行吗?签入开发工具 (F12)。如果您有旧的 DOCTYPE,您可能会在 IE8/7 模式下看到您的页面,因此不支持 indexOf。如果您在 IE9 模式下运行,那么它工作得很好。
回答by oezi
your code looks right and this compatibility tableshows that IE9 should support indexOf()
and checks your actual browser for compatibility.
您的代码看起来正确,此兼容性表显示 IE9 应该支持indexOf()
并检查您的实际浏览器的兼容性。
try to open it and take a look at your result. maybe you're running you IE in compatibility mode for IE7/8 or something else.
试着打开它看看你的结果。也许您在 IE7/8 或其他东西的兼容模式下运行 IE。
this jsfiddleworks in my IE9 - please try that, too.
这个 jsfiddle可以在我的 IE9 中使用 - 请也尝试一下。
回答by Fabian
It might help if you declare the array explicitly:
如果您显式声明数组可能会有所帮助:
var a = new Array(1,2,3);
a.indexOf(2);