javascript - JavaScript 的数组过滤方法在 IE 7 中不起作用

标签 javascript jquery asp.net web internet-explorer-7

我在网络应用程序中使用了一个jQuery插件,该插件在IE 10和11中运行良好。但在 IE 7 中失败了。 当我调查时,我发现 filter 方法的值是 undefined。 失败的代码行如下:

if (splitters.filter(Boolean).length === 0) {

我正在使用jQuery 1.8.3

最佳答案

这是 JavaScript filter() 方法,仅在 IE 9+ as per the MDN documentation 中支持

<小时/> Check polyfill option from MDN for older browser .

if (!Array.prototype.filter) {
  Array.prototype.filter = function(fun/*, thisArg*/) {
    'use strict';

    if (this === void 0 || this === null) {
      throw new TypeError();
    }

    var t = Object(this);
    var len = t.length >>> 0;
    if (typeof fun !== 'function') {
      throw new TypeError();
    }

    var res = [];
    var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
    for (var i = 0; i < len; i++) {
      if (i in t) {
        var val = t[i];

        // NOTE: Technically this should Object.defineProperty at
        //       the next index, as push can be affected by
        //       properties on Object.prototype and Array.prototype.
        //       But that method's new, and collisions should be
        //       rare, so use the more-compatible alternative.
        if (fun.call(thisArg, val, i, t)) {
          res.push(val);
        }
      }
    }

    return res;
  };
}

关于javascript - JavaScript 的数组过滤方法在 IE 7 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37719487/

相关文章:

java - jquery和java中解析json

c# - 在C#中为全文搜索添加参数问题

asp.net - ASP.NET 应用程序加载时间缓慢 - 我可以跟踪/追踪/计时整个加载周期吗?

javascript - 我如何使用回车键来提交用户名和密码

c# - 在应用程序池中运行我的邮件队列是否错误?

javascript - 如何正确使用字段对数组进行排序

javascript - 无法在vuejs中的数据方法中访问 Prop 值

javascript - 从 HTML 数据运行 Javascript

javascript - 如果存在,则在字符第 3 次出现后替换所有内容

javascript - 使用 Session.Timeout 在 ASP 中不活动后重定向