为什么javascript包含属性在chrome浏览器中不起作用?我在javascript中试过包含Property.它在Mozila Firefox浏览器中工作正常.但它无法在Chrome浏览器中运行.如何解决这个问题?
链接: http ://www.codingslover.com/2014/11/why-javascript-contains-property-is-not.html
var ClearFilterValue='family Schools';
if(ClearFilterValue.contains("family")== true) {
alert('Success');
}
Run Code Online (Sandbox Code Playgroud)
Pra*_*eek 35
indexof返回字符串的位置.如果未找到,则返回-1:
var ClearFilterValue = 'family Schools';
alert(ClearFilterValue.indexOf("family") != -1);
Run Code Online (Sandbox Code Playgroud)
Doo*_*nob 13
containsChrome不支持,但您可以使用polyfill:
if (!String.prototype.contains) {
String.prototype.contains = function(s) {
return this.indexOf(s) > -1
}
}
'potato'.contains('tat') // true
'potato'.contains('tot') // false
Run Code Online (Sandbox Code Playgroud)
实际上String.containsChrome不支持MDN
以下是解决问题的方法:
填充工具
您可以轻松地填充此方法:
if (!('contains' in String.prototype)) String.prototype.contains = function (str, startIndex) {
return -1 !== String.prototype.indexOf.call(this, str, startIndex);
};
var ClearFilterValue = 'family Schools';
if (ClearFilterValue.contains("family") == true) {
alert('Success');
}
Run Code Online (Sandbox Code Playgroud)
演示:小提琴
| 归档时间: |
|
| 查看次数: |
31739 次 |
| 最近记录: |