浏览器兼容性" inArray"功能(无jQuery的) [英] Crossbrowser "inArray" function (without jQuery)

查看:304
本文介绍了浏览器兼容性" inArray"功能(无jQuery的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道的坏浏览器兼容性兼容性<一的href=\"https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf#Browser_compatibility\"相对=nofollow> array.indexOf() 。但现在的我,我需要找到一种方法来达到同样的事情,但不使用previous方法。

I wasn't aware of the bad crossbrowser compatibility of array.indexOf() . But now that I am, I need to find a way to achieve the same thing but without using the previous method.

我试着谷歌上搜索了一阵,但没有发现真正令人信服的答案。现在,我与循环做(但这是缓慢的,我相信有更好的方法)

I tried googling for a while, but found no real convincing answers. For now, I am doing it with loops (but this is slow and I am sure there are better ways)

旁注:


  • 我不能使用jQuery或任何其他库/框架。

  • 它不一定需要返回的索引(一个简单的真/假
    将确定)

我认为这是没有必要分享我的code,既然你都知道数组回路检查的样子(再加上它会降低你的IQ)

I thought it is not necessary to share my code, since you all know how array-loop check looks like (plus it will lower your IQ)

推荐答案

下面是inArray如何jQuery中实现的:

Here is how inArray is implemented in jQuery:

function inArray(elem, array, i) {
    var len;
    if ( array ) {
        if ( array.indexOf ) {
            return array.indexOf.call( array, elem, i );
        }
        len = array.length;
        i = i ? i < 0 ? Math.max( 0, len + i ) : i : 0;
        for ( ; i < len; i++ ) {
            // Skip accessing in sparse arrays
            if ( i in array && array[ i ] === elem ) {
                return i;
            }
        }
    }
    return -1;
}

您不能使用jQuery,但为什么不使用其实施? : - )

You can not use jQuery but why not use their implementation? :-)

祝商祺!

这篇关于浏览器兼容性&QUOT; inArray&QUOT;功能(无jQuery的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆