Javascript .includes() 在 Internet Explorer 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36574351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.includes() not working in Internet Explorer
提问by Carlosss
This code does not work in internet explorer. Any alternative?
此代码在 Internet Explorer 中不起作用。有什么替代方案吗?
"abcde".includes("cd")
回答by Phillip
String.prototype.includes
is, as you write, not supported in Internet Explorer (or Opera).
String.prototype.includes
正如您所写,Internet Explorer(或 Opera)不支持。
Instead you can use String.prototype.indexOf
. #indexOf
returns the index of the first character of the substring if it is in the string, otherwise it returns -1
. (Much like the Array equivalent)
相反,您可以使用String.prototype.indexOf
. #indexOf
如果它在字符串中,则返回子字符串的第一个字符的索引,否则返回-1
. (很像 Array 等价物)
var myString = 'this is my string';
myString.indexOf('string');
// -> 11
myString.indexOf('hello');
// -> -1
MDN has a polyfill for includes
using indexOf
: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Polyfill
MDN 有一个 polyfill 用于includes
使用indexOf
:https: //developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Polyfill
EDIT: Opera supports includes
as of version 28.
编辑:Opera支持includes
为版本28。
EDIT 2: Current versions of Edge supports the method. (as of 2019)
编辑 2:Edge 的当前版本支持该方法。(截至 2019 年)
回答by Prasun
Or just put this in a Javascript file and have a good day :)
或者只是把它放在一个 Javascript 文件中,祝你有美好的一天:)
String.prototype.includes = function (str) {
var returnValue = false;
if (this.indexOf(str) !== -1) {
returnValue = true;
}
return returnValue;
}
回答by deeveeABC
includes() is not supported by most browsers. Your options are either to use
大多数浏览器不支持includes()。您的选择是使用
-polyfill from MDNhttps://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/includes
-来自 MDN 的 polyfill https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/includes
or to use
或使用
-indexof()
-指数()
var str = "abcde";
var n = str.indexOf("cd");
Which gives you n=2
这给你 n=2
This is widely supported.
这是得到广泛支持的。
回答by Joe
Problem:
问题:
Try running below(without solution) from Internet Explorerand see the result.
尝试从 Internet Explorer运行以下(无解决方案)并查看结果。
console.log("abcde".includes("cd"));
Solution:
解决方案:
Now run below solution and check the result
现在运行下面的解决方案并检查结果
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"));
回答by Skwall
This one may be better and shorter:
这个可能更好更短:
function stringIncludes(a, b) {
return a.indexOf(b) >= 0;
}
回答by Alexei
I had the same problem when working in Angular 5. In order to make it work directly without writing a polyfill yourself, just add the following line to polyfills.ts file:
我在 Angular 5 中工作时遇到了同样的问题。为了使其直接工作而无需自己编写 polyfill,只需将以下行添加到 polyfills.ts 文件中:
import "core-js/es7/array"
Also, tsconfig.json
lib section might be relevant:
此外,tsconfig.json
lib 部分可能是相关的:
"lib": [
"es2017",
"dom"
],
回答by niket bajaj
For react:
对于反应:
import 'react-app-polyfill/ie11';
import 'core-js/es5';
import 'core-js/es6';
import 'core-js/es7';
Issue resolve for - includes(), find(), and so on..
问题解决 - include()、find() 等..
回答by Abel Valdez
It works for me:
这个对我有用:
function stringIncludes(a, b) {
return a.indexOf(b) !== -1;
}
回答by mikeTeixeira88
If you want to keep using the Array.prototype.include()
in javascript you can use this script:
github-script-ie-includeThat converts automatically the include() to the match() function if it detects IE.
如果你想继续使用Array.prototype.include()
javascript 你可以使用这个脚本:
github-script-ie-include如果它检测到 IE,它会自动将 include() 转换为 match() 函数。
Other option is using always thestring.match(Regex(expression))
其他选项是始终使用string.match(Regex(expression))
回答by mesvil
You can do the same with !! and ~ operators
你也可以这样做!!和 ~ 运算符
var myString = 'this is my string';
!!~myString.indexOf('string');
// -> true
!!~myString.indexOf('hello');
// -> false
here's the explanation of the two operators (!! and ~ )
这是两个运算符(!! 和 ~ )的解释
What is the !! (not not) operator in JavaScript?
https://www.joezimjs.com/javascript/great-mystery-of-the-tilde/
https://www.joezimjs.com/javascript/great-mystery-of-the-tilde/