我试图使用contains来找出一个短语是否出现在字符串中,下面的代码在FF和Chrome中工作得很好,但是IE8-10返回一个错误。
SCRIPT438:对象不支持属性或方法“包含”
var str = "This is a string";
if(str.contains("string")){
alert('Yes'};
}不知道为什么IE会抛出一个错误,所以任何帮助都将不胜感激。
发布于 2014-11-03 17:58:02
.contains()函数是一个早期Internet版本不支持的ES2015功能。
MDN页面有一个多填充:
if ( !String.prototype.contains ) {
String.prototype.contains = function() {
return String.prototype.indexOf.apply( this, arguments ) !== -1;
};
}这样的问题一般指南:在Google搜索框中键入MDN something。如果你找不到结果,那么“某物”可能在JavaScript宇宙中就不存在了。如果你这样做了,那么你很有可能找到你在那里寻找的答案。
https://stackoverflow.com/questions/26720077
复制相似问题