javascript - .includes() 是未定义的 IE

标签 javascript jquery internet-explorer

我在我的代码中使用 .include() 这在其他浏览器上工作正常但在 IE 中不工作。

if (feauture2_1_title.includes("Phase")) {

}
  • 如果 IE 不支持任何替代品?

最佳答案

这是一个新功能。 The MDN提供一个 polyfill(你可以在自己的代码中包含一些代码来替换旧浏览器中的标准功能):

if (!Array.prototype.includes) {
  Array.prototype.includes = function(searchElement /*, fromIndex*/ ) {
    'use strict';
    var O = Object(this);
    var len = parseInt(O.length) || 0;
    if (len === 0) {
      return false;
    }
    var n = parseInt(arguments[1]) || 0;
    var k;
    if (n >= 0) {
      k = n;
    } else {
      k = len + n;
      if (k < 0) {k = 0;}
    }
    var currentElement;
    while (k < len) {
      currentElement = O[k];
      if (searchElement === currentElement ||
         (searchElement !== searchElement && currentElement !== currentElement)) {
        return true;
      }
      k++;
    }
    return false;
  };
}

但大多数情况下,您会对其中一种解决方案感到满意:

feauture2_1_title.indexOf("Phase")!==-1

/Phase/.test(feauture2_1_title)

关于javascript - .includes() 是未定义的 IE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31913108/

相关文章:

javascript - 使用 jquery 突出显示当前菜单项

jQuery datepicker onSelect 调用 .append() 不起作用

html - 列表样式位置为 : inside in Internet explorer 的列表项出现意外间距

javascript - 方法将最终总和四舍五入到小数点后两位 - jquery

javascript - 有没有一种简单的方法可以将 jquery 代码转换为 javascript?

jquery - colorbox.close Firebug 错误

php - 在移动浏览器中显示使用 mpdf inline 生成的 pdf

html - 内联样式不在 IE7、8、9 中呈现

javascript - 为什么在循环中动态添加.onclick到img元素需要return function()?

javascript - 如何使我的 C 程序运行得更快?