解决浏览器兼容问题
来源:互联网 发布:淘宝店铺宝贝卖点怎么写 编辑:程序博客网 时间:2024/06/04 00:43
浏览器兼容问题
- indexOf 在ECMA-262 标准 的第5版中被加入,但并非所有的浏览器都支持该方法。(在低版本浏览器下出现对象不支持此属性或方法)
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Demo1</title></head><script> var a = [2, 9, 9]; alert(a.indexOf(9));</script></html>
解决方案(其它函数同理)
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Demo2</title></head><body></body><script> // Production steps of ECMA-262, Edition 5, 15.4.4.14 // Reference: http://es5.github.io/#x15.4.4.14 if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(searchElement, fromIndex) { var k; // 1. Let O be the result of calling ToObject passing // the this value as the argument. if (this == null) { throw new TypeError('"this" is null or not defined'); } var O = Object(this); // 2. Let lenValue be the result of calling the Get // internal method of O with the argument "length". // 3. Let len be ToUint32(lenValue). var len = O.length >>> 0; // 4. If len is 0, return -1. if (len === 0) { return -1; } // 5. If argument fromIndex was passed let n be // ToInteger(fromIndex); else let n be 0. var n = +fromIndex || 0; if (Math.abs(n) === Infinity) { n = 0; } // 6. If n >= len, return -1. if (n >= len) { return -1; } // 7. If n >= 0, then Let k be n. // 8. Else, n<0, Let k be len - abs(n). // If k is less than 0, then let k be 0. k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); // 9. Repeat, while k < len while (k < len) { // a. Let Pk be ToString(k). // This is implicit for LHS operands of the in operator // b. Let kPresent be the result of calling the // HasProperty internal method of O with argument Pk. // This step can be combined with c // c. If kPresent is true, then // i. Let elementK be the result of calling the Get // internal method of O with the argument ToString(k). // ii. Let same be the result of applying the // Strict Equality Comparison Algorithm to // searchElement and elementK. // iii. If same is true, return k. if (k in O && O[k] === searchElement) { return k; } k++; } return -1;};}</script><script> var a = [2, 9, 9]; alert(a.indexOf(9));</script></html>
了解更多
- 学习笔记
阅读全文
0 0
- opacity浏览器兼容问题解决
- 20150909解决浏览器兼容问题
- 浏览器兼容问题解决方式
- 浏览器兼容问题的解决
- 解决浏览器兼容问题
- 这样解决浏览器不兼容问题
- 这样解决浏览器不兼容问题
- 这样解决浏览器不兼容问题
- JavaScript解决常见浏览器兼容问题
- 这样解决浏览器不兼容问题 .
- 这样解决浏览器不兼容问题
- JavaScript解决常见浏览器兼容问题
- 这样解决浏览器不兼容问题
- 常见浏览器兼容问题与解决
- Ajax解决IE浏览器兼容问题
- javascript Date() 浏览器兼容问题解决
- 解决JS中的浏览器兼容问题
- 解决旧版 IE 浏览器兼容问题
- php-fpm参数优化
- 【智能城市】能源互联网:控制的机遇与挑战
- 【智能制造】智能制造环境中工业通信技术的规范和实现途径
- 【人工智能】人工智能在深度学习领域的前世今生
- kali ubantu 中输入密码以解锁密钥环的解决
- 解决浏览器兼容问题
- Nginx 配置文件 nginx.conf 详解
- PHP解析大型Excel表格的库:box/spout
- 编译原理复习6
- Codeforces Round #446 (Div. 2)
- 【代码笔记】iOS-HTTPQueue下载图片
- 配电箱干什么用
- 5步让你入门MongoDB!
- docker/virtualenv