JavaScript在ie8的兼容处理(如forEach、indexOf..)
记一次项目中遇到得坑吧。。
1.IE8不兼容forEach–作为一个遍历神器 不能用怎么行  0.01
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27//解决办法 在js文件中添加下面的代码:
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++;  
    }  
  };  
}  
//要加在调用forEach之前
2.IE8不兼容indexOf1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19if (!Array.prototype.indexOf){  
        Array.prototype.indexOf = function(elt /*, from*/){  
        var len = this.length >>> 0;  
        var from = Number(arguments[1]) || 0;  
        from = (from < 0)  
             ? Math.ceil(from)  
             : Math.floor(from);  
        if (from < 0)  
          from += len;  
        for (; from < len; from++)  
        {  
          if (from in this &&  
              this[from] === elt)  
            return from;  
        }  
        return -1;  
      };  
    }  
//同样加在调用indexOf之前