HTML5网页设计

javaScript

让ie8支持foreach方法

时间:2017-01-03 10:03来自:未知作者:user点击:
JS代码中添加以下代码,让IE8支持forEach:
JS代码中添加以下代码,让IE8支持forEach:
if ( !Array.prototype.forEach ) {

    Array.prototype.forEach = function forEach( callback, thisArg ) {

        var T, k;

        if ( this == null ) {
            throw new TypeError( "this is null or not defined" );
        }
        var O = Object(this);
        var len = O.length >>> 0;
        if ( typeof callback !== "function" ) {
            throw new TypeError( callback + " is not a function" );
        }
        if ( arguments.length > 1 ) {
            T = thisArg;
        }
        k = 0;

        while( k < len ) {

            var kValue;
            if ( k in O ) {

                kValue = O[ k ];
                callback.call( T, kValue, k, O );
            }
            k++;
        }
    };
}

当前文章地址:http://www.shejicool.com/web/javaScript/703.html

阅完本篇文章?您还可以: 收藏本文 文章挑错 朋友推荐