简体   繁体   English

indexOf不是Firefox中的一个函数,Opera可以在IE中工作,indexOf在javascript中替代测试字符串包含?

[英]indexOf is not a function in Firefox, Opera but works in IE, indexOf alternative in javascript to test string contains?

Getting an error using indexOf call in Javascript on Firefox and Opera. 在Firefox和Opera上的Javascript中使用indexOf调用获取错误。 Works fine in IE. 在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 for事件OnRightClick失败,异常:row.id.indexOf不是函数

I'm testing that a string contains another string in Javascript and using the indexOf function of a string. 我正在测试一个字符串在Javascript中包含另一个字符串并使用字符串的indexOf函数。 The calls however are being made in JQuery functions. 然而,调用是在JQuery函数中进行的。 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? 是否有替代方法在Javascript中使用indexOf来测试字符串是否包含另一个字符串? Is there a workaround for this problem? 这个问题有解决方法吗?

String.indexOf is perfectly OK in all browsers. String.indexOf在所有浏览器中都完全正常。 I assume the id property of your row object is no string (nor array, btw, because indexOf is also defined on arrays (except for IE)) 我假设你的对象的id属性是没有字符串(也不是数组,顺便说一句,因为indexOf也在数组上定义(IE除外))

indexOf is not okay for IE prior to IE9. 对于IE9之前的IE,indexOf不合适。 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. 如果您希望代码在<9中工作,则应该在公共js文件中为不兼容的浏览器定义可以放入每个页面的方法。 See this thread for more details. 有关详细信息,请参阅此主题 The code is taken from Mozilla 代码取自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;
    }
}

indexOf() is ok for all browsers. indexOf()适用于所有浏览器。 It is designed for both, String and Array, see this: http://jsfiddle.net/SquTp/ 它专为String和Array而设计,请参阅: http//jsfiddle.net/SquTp/

There is maybe something wrong with your dom selection, or you may use it in the wrong way. 您的dom选择可能有问题,或者您可能以错误的方式使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM