indexOf 不是 Firefox、Opera 中的函数,但可以在 IE 中使用,javascript 中的 indexOf 替代测试字符串包含吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10992766/
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
indexOf is not a function in Firefox, Opera but works in IE, indexOf alternative in javascript to test string contains?
提问by user840930
Getting an error using indexOf call in Javascript on Firefox and Opera. Works fine in IE.
在 Firefox 和 Opera 上使用 Javascript 中的 indexOf 调用出错。在 IE 中工作正常。
Following is the error message:
以下是错误消息:
Action
行动
function anonymous(Grid, Row, Col, Event) {
return Grid.ActionShowPopupMenu();
}
for event OnRightClick failed with exception: row.id.indexOf is not a function
事件 OnRightClick 失败,异常:row.id.indexOf 不是函数
I'm testing that a string contains another string in Javascript and using the indexOf function of a string. The calls however are being made in JQuery functions. Perhaps that is the reason for the problem? Is there an alternative to using indexOf in Javascript to test if a string contains another string? Is there a workaround for this problem?
我正在测试一个字符串是否包含 Javascript 中的另一个字符串并使用字符串的 indexOf 函数。然而,调用是在 JQuery 函数中进行的。也许这就是问题的原因?是否有替代在 Javascript 中使用 indexOf 来测试字符串是否包含另一个字符串的替代方法?这个问题有解决方法吗?
采纳答案by RomainValeri
String.indexOf is perfectly OK in all browsers. I assume the idproperty of your rowobject is no string (nor array, btw, because indexOf is also defined on arrays (except for IE))
String.indexOf 在所有浏览器中都可以。我假设您的行对象的id属性不是字符串(也不是数组,顺便说一句,因为 indexOf 也在数组上定义(IE 除外))
回答by ronan_mac
indexOf is not okay for IE prior to IE9. If you want your code to work in ie < 9, you should define the method for non-compliant browsers in a common js file that can be dropped into every page. See this threadfor more details. The code is taken from Mozilla
indexOf 不适用于 IE9 之前的 IE。如果你想让你的代码在 ie < 9 中工作,你应该在一个可以放入每个页面的通用 js 文件中定义不兼容浏览器的方法。有关更多详细信息,请参阅此线程。代码取自Mozilla
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
"use strict";
if (this == null) {
throw new TypeError();
}
var t = Object(this);
var len = t.length >>> 0;
if (len === 0) {
return -1;
}
var n = 0;
if (arguments.length > 1) {
n = Number(arguments[1]);
if (n != n) { // shortcut for verifying if it's NaN
n = 0;
} else if (n != 0 && n != Infinity && n != -Infinity) {
n = (n > 0 || -1) * Math.floor(Math.abs(n));
}
}
if (n >= len) {
return -1;
}
var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
for (; k < len; k++) {
if (k in t && t[k] === searchElement) {
return k;
}
}
return -1;
}
}
回答by Someth Victory
indexOf() is ok for all browsers. It is designed for both, String and Array, see this: http://jsfiddle.net/SquTp/
indexOf() 适用于所有浏览器。它是为字符串和数组设计的,请参见:http: //jsfiddle.net/SquTp/
There is maybe something wrong with your dom selection, or you may use it in the wrong way.
您的 dom 选择可能有问题,或者您可能以错误的方式使用它。