We all know that forEach in IE8 and earlier versions does not support js. How can we make IE8 or earlier versions support forEach? This article introduces the solution that IE8 does not support forEach. For more information about the code farmers, see. First, let's determine whether the browser supports js forEach. The Code is as follows:
If (typeof Array. prototype. forEach! = 'Function') {// not supported. In this case, we need to define a function similar to the forEach function .}
If the browser does not support forEach, We need to write a foreach function by ourselves. For the specific function body, see the following code:
Function (callback) {for (var I = 0; I <this. length; I ++) {callback. apply (this, [this [I], I, this]) ;}}
Therefore, the solution to IE8 does not support forEach should be as follows:
If (typeof Array. prototype. forEach! = 'Function') {Array. prototype. forEach = function (callback) {for (var I = 0; I <this. length; I ++) {callback. apply (this, [this [I], I, this]) ;}};}
If you use the js plug-in, you only need to put the changed code at the beginning of the plug-in code. In this way, all browsers support js foreach.