javascript SCRIPT5007:无法获取属性“indexOf”的值:对象在 IE 中为 null 或未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16787799/
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
SCRIPT5007: Unable to get value of the property 'indexOf': object is null or undefined in IE
提问by honey
Hi I got this error in IE. It works in all other browsers.
嗨,我在 IE 中遇到了这个错误。它适用于所有其他浏览器。
This is the line where error shows:
这是错误显示的行:
if (parseFloat(totalnumm.replace(/[^0-9-.]/g,'')) > compare_value_neww && values[x].indexOf("Custom") > -1 ).
I googled and found one solution:
我用谷歌搜索并找到了一个解决方案:
<meta http-equiv="X-UA-Compatible" content="IE=8;FF=3;OtherUA=4" />
But it did not work for me.
但它对我不起作用。
回答by Spudley
I was going to ask what version of IE you're testing this in, but since you've specified content="IE=8
in your meta tags, that's fairly irrelevant.
我想问你在什么版本的 IE 中测试这个,但因为你已经content="IE=8
在你的元标记中指定了,这相当无关紧要。
The short answer is that .indexOf
for arrays is not supported in IE8 or earlier. (That includes later IE versions in compatibility mode, so your meta tag will mean that it won't work in any IE version)
简短的回答是.indexOf
IE8 或更早版本不支持数组。(这包括兼容模式下的更高版本的 IE,因此您的元标记将意味着它在任何 IE 版本中都不起作用)
Solutions:
解决方案:
Use a library like jQuery (or similar) which supplies an
.inArray()
method that you can use instead.You'll then need to change your code from using
var.indexOf(x)
to$.inArray(var,x)
Pick this solution if you're already using jQuery (or another library that has this feature).
Use a polyfill library like this onethat adds the standard
.indexOf
method to the Array prototype.This should allow you to keep your existing code unchanged; just include the library.
Use this solution if you are happy to use a library but you haven't got one already installed that would help.
Write your own function that does the same job using a
for()
loop.This is a complete change in how you find things in your arrays, but does mean you don't need to use any extra libraries.
Use this solution if you don't want to (or can't, for whatever reason) use a third-party library.
Remove your IE8 meta tag (it's pretty bad anyway, so that's a good idea) and only support your site for users with IE9 or better.
Use this solution if you're happy to stop supporting older IE versions.
In fact, it would be a good idea to do this anyway; there's no good reason to be using the meta tag to force IE into compatibility mode. Better to set it to
content="IE=edge"
. That will remove the problem entirely for newer IE versions. If you do need to support IE8 or earlier, then this solution won't solve the problem and you'll need to also use one of the other solutions above, but I'd still recommend doing it anyway, because as things stand you are deliberately removing features from newer IE versions for no good reason.
使用像 jQuery(或类似的)这样的库,它提供了一个
.inArray()
你可以使用的方法。然后,您需要将代码从 using 更改
var.indexOf(x)
为$.inArray(var,x)
如果您已经在使用 jQuery(或其他具有此功能的库),请选择此解决方案。
使用像这样的 polyfill 库将标准
.indexOf
方法添加到 Array 原型。这应该允许您保持现有代码不变;只包括图书馆。
如果您乐于使用某个库,但尚未安装有帮助的库,请使用此解决方案。
使用
for()
循环编写您自己的函数来完成相同的工作。这完全改变了您在数组中查找内容的方式,但这确实意味着您不需要使用任何额外的库。
如果您不想(或不能,无论出于何种原因)使用第三方库,请使用此解决方案。
删除您的 IE8 元标记(无论如何它很糟糕,所以这是一个好主意)并且只为使用 IE9 或更高版本的用户支持您的站点。
如果您乐于停止支持较旧的 IE 版本,请使用此解决方案。
事实上,无论如何这样做是个好主意;没有充分的理由使用元标记来强制 IE 进入兼容模式。最好将其设置为
content="IE=edge"
. 对于较新的 IE 版本,这将完全消除该问题。如果您确实需要支持 IE8 或更早版本,那么此解决方案将无法解决问题,您还需要使用上述其他解决方案之一,但我仍然建议您这样做,因为就目前情况而言,您是无缘无故故意从较新的 IE 版本中删除功能。