我正在使用Javascript编写一个搜索函数,它几乎完成了,在chrome上运行良好,但在资源管理器中就不行了。我使用了资源管理器开发工具,它告诉我这是它不能处理的行:
if (compareElement.indexOf(compareString)!== -1 &&
processFindings.indexOf(processes[i]) === -1)
错误是对象不支持此属性或方法。
这一行过去是这样写的:
if(compareElement.indexOf(compareString)!== -1)
它工作得很好,只是当我添加了explorer无法处理的额外条件时。
有人能帮我吗?
发布于 2012-09-05 08:16:17
并非所有版本的IE都有用于数组的.indexOf()
方法。不过,它有一个polyfill,它工作得很好。
查看此处:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf
但是,这可能会导致不能正确迭代数组的代码出现问题。
发布于 2012-09-05 08:27:58
您可以复制该方法(如果它不在那里),但是使用原型需要在调用它之前它已经存在。举个例子:
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(searchElement,fromIndex) {
fromIndex = parseInt(fromIndex || 0);
for (var i = 0; i < this.length; i++) {
if (this[i] == searchElement) return i;
}
return -1;
}
}
https://stackoverflow.com/questions/12272919
复制相似问题