标签:style blog http color io os 使用 ar java
1. js 数组循环遍历。
<!--Add by oscar999--> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Author" CONTENT="oscar999"> </HEAD> <BODY> <script> var arryAll = []; arryAll.push(1); arryAll.push(2); arryAll.push(3); arryAll.push(4); //匿名方式 arryAll.forEach(function(e){ alert(e); }) function t1(arg){alert(arg);} //非匿名方式 arryAll.forEach(t1,arryAll); </script> </BODY> </HTML>
但是以上,代码在IE中却无法正常工作。
alert(Array.prototype.forEach);
执行以上这句得到的是 "undefined", 也就是说在IE 中 Array 没有forEach的方法。
//Array.forEach implementation for IE support.. //https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach if (!Array.prototype.forEach) { Array.prototype.forEach = function(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; // Hack to convert O.length to a UInt32 if ({}.toString.call(callback) != "[object Function]") { throw new TypeError(callback + " is not a function"); } if (thisArg) { T = thisArg; } k = 0; while (k < len) { var kValue; if (k in O) { kValue = O[k]; callback.call(T, kValue, k, O); } k++; } }; }
<!--Add by oscar999--> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Author" CONTENT="oscar999"> </HEAD> <BODY> <script> if (!Array.prototype.forEach) { Array.prototype.forEach = function(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; // Hack to convert O.length to a UInt32 if ({}.toString.call(callback) != "[object Function]") { throw new TypeError(callback + " is not a function"); } if (thisArg) { T = thisArg; } k = 0; while (k < len) { var kValue; if (k in O) { kValue = O[k]; callback.call(T, kValue, k, O); } k++; } }; } var arryAll = []; arryAll.push(1); arryAll.push(2); arryAll.push(3); arryAll.push(4); arryAll.push(5); arryAll.push(6); arryAll.push(7); var arrySpecial = []; arryAll.forEach(function(e){ if(e%2==0) { arrySpecial.push(e); }else if(e%3==0) { arrySpecial.push(e); } }) </script> </BODY> </HTML>
arryAll.forEach(function(e){ if(e%2==0) { www.2cto.com arrySpecial.push(e); return; } if(e%3==0) { arrySpecial.push(e); return; } })
var arryAll = []; arryAll.push(1); arryAll.push(2); arryAll.push(3); arryAll.push(4); arryAll.push(5); arryAll.push(6); arryAll.push(7); arryAll.forEach(function(e){ alert(e); if(e>3) return false; });
标签:style blog http color io os 使用 ar java
原文地址:http://www.cnblogs.com/mq0036/p/4015689.html