多语言展示
当前在线:1188今日阅读:176今日分享:34

原生js兼容(indexOf,forEach,currentStyle)

经常会用到原生JS来写前端。。。但是原生JS的一些方法在适应各个浏览器的时候写法有的也不怎么一样的。。。
方法/步骤
1

indexOf兼容方法if(!Array.indexOf){         Array.prototype.indexOf = function(obj){                   for(var i=0; i

2

forEach兼容方法if (!Array.prototype.forEach) {             Array.prototype.forEach = function(fun /*, thisp*/){                      var len = this.length;                     if (typeof fun != "function")                        throw new TypeError();                     var thisp = arguments[1];                      for (var i = 0; i < len; i++){                             if (i in this)                                fun.call(thisp, this[i], i, this);                       }              };  }

3

currentStyle兼容方法HTMLElement.prototype.__defineGetter__("currentStyle", function () {    return this.ownerDocument.defaultView.getComputedStyle(this, null);});

推荐信息