javascript - 为什么我的 indexOf 语句会产生错误

标签 javascript

我正在尝试使用 .indexOf 来查明数组是否包含字符串。某事导致错误,但我不知道是什么。我确信这是一件真正简单的事情,但我不懂这种语言,所以看不到它。怎么了?

//this code creates an error in IE
var myMines=new Array();
var newMine="22";
var myVar = myMines.indexOf(newMine);   //when I comment out this line, the page does not generate an error

最佳答案

您是否偶然使用 IE 8(或更低版本)? Array.indexOf 仅适用于 IE 9+、Firefox 3+ 和 Chrome 7+。

来源:http://kangax.github.com/es5-compat-table/

Array.indexOf 的文档(还包括在不支持它的浏览器中实现它的函数):https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf

关于javascript - 为什么我的 indexOf 语句会产生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10016492/

相关文章:

java - 如何在 JSF 中从 Java 代码添加 JS 文件?

javascript - jQuery 隐藏和显示动画错误

javascript - Angular2+ : how does NgModel/NgControl internally handle updates from view to model?

javascript - 过滤数组并比较但跳过空值

javascript - 在 Javascript 中为数组中的数组推送语法

javascript - 如何将字符串添加到浏览器的自动完成弹出窗口?

javascript - .htaccess 规则使 sm2 无法在 Android 和 iPad 上加载我网站的 mp3

javascript - 如何在 PHP 中获取日期输出

javascript - 我在 ng-keyup 上调用一个函数在数据库中搜索,得到结果后我在 DIV 中显示结果..?

javascript - 如何编码cookie数据