Home  >  Article  >  Web Front-end  >  js IE8不支持forEach的解决方案

js IE8不支持forEach的解决方案

WBOY
WBOYOriginal
2016-06-01 09:54:302855browse

首先我们先来判断一下浏览器是否支持js的forEach,代码如下:

<code class="language-javascript">if (typeof Array.prototype.forEach != 'function') {
    //不支持,此时我们需要自己定义一个类似forEach功能的函数。
}</code>

如果浏览器不支持forEach,我们就需要自己写一个foreach功能的函数。具体函数体请看下面代码:

<code class="language-javascript">function(callback){
      for (var i = 0; i </code>

 

所以,解决IE8不支持forEach的方法应该是这样的:

<code class="language-javascript">if (typeof Array.prototype.forEach != 'function') {
    Array.prototype.forEach = function(callback){
      for (var i = 0; i </code>

如果你使用了js的插件,只需要将改代码放在插件代码开头即可。这样所以浏览器都支持js的foreach了。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn