.includes()在Internet Explorer中不起作用 [英] .includes() not working in Internet Explorer
问题描述
此代码在Internet Explorer中不起作用.还有其他选择吗?
This code does not work in internet explorer. Any alternative?
"abcde".includes("cd")
在撰写本文时,
推荐答案
String.prototype.includes
在Internet Explorer(或Opera)中不受支持.
String.prototype.includes
is, as you write, not supported in Internet Explorer (or Opera).
相反,您可以使用String.prototype.indexOf
. #indexOf
返回子字符串的第一个字符的索引(如果它在字符串中),否则返回-1
. (非常类似于Array)
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)
var myString = 'this is my string';
myString.indexOf('string');
// -> 11
myString.indexOf('hello');
// -> -1
MDN使用indexOf
为includes
提供了一个polyfill: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Polyfill
MDN has a polyfill for includes
using indexOf
: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Polyfill
从版本28 开始,Opera支持includes
.
Opera supports includes
as of version 28.
Edge的当前版本支持该方法. (截至2019年)
EDIT 2: Current versions of Edge supports the method. (as of 2019)
这篇关于.includes()在Internet Explorer中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!