浏览器兼容性问题是在实际开发中容易忽略而又最重要的一部分。我们在讲老版本浏览器兼容问题之前,首先要了解什么是能力检测,它是来检测浏览器有没有这种能力,即判断当前浏览器是否支持要调用的属性或者方法。下面做了一些简短的介绍。
// 使用指定的函数测试所有元素,并创建一个包含所有通过测试的元素的新数组
1 // 兼容旧环境 2 if (!Array.prototype.filter) 3 { 4 Array.prototype.filter = function(fun /*, thisArg */) 5 { 6 "use strict"; 7 if (this === void 0 || this === null) 8 throw new TypeError(); 9 var t = Object(this); 10 var len = t.length >>> 0; 11 if (typeof fun !== "function") 12 throw new TypeError(); 13 var res = []; 14 var thisArg = arguments.length >= 2 ? arguments[1] : void 0; 15 for (var i = 0; i < len; i++) 16 { 17 if (i in t) 18 { 19 var val = t[i]; 20 // NOTE: Technically this should Object.defineProperty at 21 // the next index, as push can be affected by 22 // properties on Object.prototype and Array.prototype. 23 // But that method's new, and collisions should be 24 // rare, so use the more-compatible alternative. 25 if (fun.call(thisArg, val, i, t)) 26 res.push(val); 27 } 28 } 29 return res; 30 }; 31 }// 遍历数组
1 //兼容旧环境 2 // Production steps of ECMA-262, Edition 5, 15.4.4.18 3 // Reference: http://es5.github.io/#x15.4.4.18 4 if (!Array.prototype.forEach) { 5 Array.prototype.forEach = function(callback, thisArg) { 6 var T, k; 7 if (this == null) { 8 throw new TypeError(' this is null or not defined'); 9 } 10 // 1. Let O be the result of calling toObject() passing the 11 // |this| value as the argument. 12 var O = Object(this); 13 // 2. Let lenValue be the result of calling the Get() internal 14 // method of O with the argument "length". 15 // 3. Let len be toUint32(lenValue). 16 var len = O.length >>> 0; 17 // 4. If isCallable(callback) is false, throw a TypeError 18 exception. // See: http://es5.github.com/#x9.11 19 if (typeof callback !== "function") { 20 throw new TypeError(callback + ' is not a function'); 21 } 22 // 5. If thisArg was supplied, let T be thisArg; else let 23 // T be undefined. 24 if (arguments.length > 1) { 25 T = thisArg; 26 } 27 // 6. Let k be 0 28 k = 0; 29 // 7. Repeat, while k < len 30 while (k < len) { 31 var kValue; 32 // a. Let Pk be ToString(k). 33 // This is implicit for LHS operands of the in operator 34 // b. Let kPresent be the result of calling the HasProperty 35 // internal method of O with argument Pk. 36 // This step can be combined with c 37 // c. If kPresent is true, then 38 if (k in O) { 39 // i. Let kValue be the result of calling the Get internal 40 // method of O with argument Pk. 41 kValue = O[k]; 42 // ii. Call the Call internal method of callback with T as 43 // the this value and argument list containing kValue, k, and O. 44 callback.call(T, kValue, k, O); 45 } 46 // d. Increase k by 1. 47 k++; 48 } 49 // 8. return undefined 50 }; 51 }// 兼容旧环境
1 var EventTools = { 2 addEventListener: function (element, eventName, listener) { 3 //能力检测 4 if(element.addEventListener) { 5 element.addEventListener(eventName, listener,false); 6 }else if(element.attachEvent) { 7 element.attachEvent("on" + eventName, listener); 8 }else{ 9 element["on" + eventName] = listener; 10 } 11 }, 12 13 // 想要移除事件,不能使用匿名函数 14 removeEventListener: function (element, eventName, listener) { 15 if(element.removeEventListener) { 16 element.removeEventListener(eventName,listener,false); 17 }else if(element.detachEvent) { //IE8以前注册.attachEvent和移除事件.detachEvent 18 element.detachEvent("on"+eventName,listener); 19 }else{ 20 element["on" + eventName] = null; 21 } 22 } 23 };
【总结】这里只是做了一部分的小结,实际开发中也还会遇到各种浏览器兼容的问题。不同浏览器在PC端和手机端也会遇到不同适配问题,这些就有待童鞋们一起去发掘总结啦~~希望能帮到大家,不足的地方请多指教啦~~~
转载于:https://www.cnblogs.com/DF-fzh/p/5408241.html
相关资源:JAVA上百实例源码以及开源项目