【问题标题】:.includes() not working in Internet Explorer.includes() 在 Internet Explorer 中不起作用
【发布时间】:2016-04-12 13:10:47
【问题描述】:

此代码在 Internet Explorer 中不起作用。有什么选择吗?

"abcde".includes("cd")

【问题讨论】:

  • 两年后IE还不支持。
  • 等待 IE 变得更好就像......等待 IE 变得更好。
  • @nueverest 你的意思是3年吧? :D
  • 有人帮大家一个忙,删除 IE 的 repo(s)。结束吧。
  • 再过 2 年 - IE 仍然不支持它

标签: javascript


【解决方案1】:

String.prototype.includes 如您所写,在 Internet Explorer(或 Opera)中不受支持。

您可以改为使用String.prototype.indexOf#indexOf如果在字符串中则返回子字符串的第一个字符的索引,否则返回-1。 (很像 Array 等价物)

var myString = 'this is my string';
myString.indexOf('string');
// -> 11

myString.indexOf('hello');
// -> -1

MDN 有一个用于 includes 的 polyfill,使用 indexOf: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Polyfill

编辑:Opera 从 version 28 开始支持 includes

编辑 2:Edge 的当前版本支持该方法。 (截至 2019 年)

【讨论】:

  • include()是IE唯一不支持的函数吗?或者还有其他IE不支持的typescript或javascript函数?
  • 如果我们需要Boolean,我们可以(myString.indexOf('string') > -1) // to get a boolean true or false
【解决方案2】:

或者只是把它放在一个 Javascript 文件中,祝你有美好的一天:)

String.prototype.includes = function (str) {
  var returnValue = false;

  if (this.indexOf(str) !== -1) {
    returnValue = true;
  }

  return returnValue;
}

【讨论】:

  • 如果你使用这个polyfill,不要用for...in迭代你的字符串,如果它是这样定义的,它会迭代String.prototype.includes
  • 短版:return this.indexOf(str) !== -1;
  • 对于数组:Array.prototype.includes = function (elt) { return this.indexOf(elt) !== -1; }
【解决方案3】:

includes() 不受大多数​​浏览器的支持。您的选择是使用

-来自 MDN 的 polyfill https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/includes

或使用

-indexof()

var str = "abcde";
var n = str.indexOf("cd");

这给你 n=2

这得到了广泛的支持。

【讨论】:

  • 如果你使用来自 MDN 的 polyfill,不要用for...in 迭代你的字符串!,如果你这样定义它会迭代String.prototype.includes。跨度>
【解决方案4】:

问题:

尝试在下面运行(无解决方案)从 Internet Explorer 并查看结果。

console.log("abcde".includes("cd"));

解决方案:

现在运行下面的解决方案并检查结果

if (!String.prototype.includes) {//To check browser supports or not
  String.prototype.includes = function (str) {//If not supported, then define the method
    return this.indexOf(str) !== -1;
  }
}
console.log("abcde".includes("cd"));

【讨论】:

    【解决方案5】:

    这个可能会更好更短:

    function stringIncludes(a, b) {
        return a.indexOf(b) >= 0;
    }
    

    【讨论】:

    • IE 不支持 indexOf
    • 它在 IE11 中运行良好。 IE10可能没有,但现在几乎没有人还在使用那个版本。
    【解决方案6】:

    我在 Angular 5 中工作时遇到了同样的问题。为了使其直接工作而无需自己编写 polyfill,只需将以下行添加到 polyfills.ts 文件中:

    import "core-js/es7/array"
    

    另外,tsconfig.json lib 部分可能是相关的:

    "lib": [
      "es2017",
      "dom"
    ],
    

    【讨论】:

    • 你我的朋友是一个完整的救星!
    【解决方案7】:

    对于反应:

    import 'react-app-polyfill/ie11';
    import 'core-js/es5';
    import 'core-js/es6';
    import 'core-js/es7';
    

    为-includes()、find()等问题解决..

    【讨论】:

      【解决方案8】:

      如果您想继续在 javascript 中使用 Array.prototype.include(),您可以使用以下脚本: github-script-ie-include 如果检测到 IE,它会自动将 include() 转换为 match() 函数。

      其他选项始终使用string.match(Regex(expression))

      【讨论】:

        【解决方案9】:

        它对我有用:

        function stringIncludes(a, b) {
              return a.indexOf(b) !== -1;
        }
        

        【讨论】:

          【解决方案10】:

          这是因为ie不支持包含所以 制作一个点函数并像 es5 中的 es6 includes() 一样使用它,如下所示:

          String.prototype.includes = function (str) {
           return this.indexOf(str) !== -1;
          }
          

          下面是字符串

          var myString = 'this is my string';
          

          检查匹配如下:

          console.log(myString.includes('string')); // true
          console.log(myString.includes('street')); //false
          

          现在你可以在 includes 方式中使用相同的 indexOf 为 ES5 添加这个

          【讨论】:

            【解决方案11】:

            你也可以用 !!和 ~ 运算符

             var myString = 'this is my string';
            
             !!~myString.indexOf('string');
             // -> true
            
             !!~myString.indexOf('hello');
             // -> false
            

            这里是两个运算符(!! 和 ~)的解释

            What is the !! (not not) operator in JavaScript?

            https://www.joezimjs.com/javascript/great-mystery-of-the-tilde/

            【讨论】:

              猜你喜欢
              • 2012-06-07
              • 2015-03-16
              • 2012-05-06
              • 2011-06-02
              • 2015-12-17
              • 2013-04-30
              • 2013-09-25
              • 1970-01-01
              • 2012-03-23
              相关资源
              最近更新 更多