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

js IE8不支持forEach的解决方案

WBOY
Release: 2016-06-01 09:54:30
Original
2929 people have browsed it

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

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

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

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

 

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

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template