javascript 为什么javascript包含属性在chrome浏览器中不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19589465/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:05:21  来源:igfitidea点击:

why javascript contains property is not working in chrome browser?

javascript

提问by Elangovan

Why javascript contains property is not working in chrome browser? I have tried that Contains Property in javascript.It is working fine in Mozila Firefox Browser. But It is not working in Chrome browser.How to fix this?

为什么javascript包含属性在chrome浏览器中不起作用?我已经尝试过在 javascript 中包含属性。它在 Mozila Firefox 浏览器中工作正常。但它在 Chrome 浏览器中不起作用。如何解决这个问题?

Link:http://www.codingslover.com/2014/11/why-javascript-contains-property-is-not.html

链接:http : //www.codingslover.com/2014/11/why-javascript-contains-property-is-not.html

var ClearFilterValue='family Schools';
if(ClearFilterValue.contains("family")== true) {
      alert('Success');
}

回答by Prateek

indexofreturns the position of the string. If not found, it will return -1:

indexof返回字符串的位置。如果没有找到,它将返回 -1

var ClearFilterValue = 'family Schools';
alert(ClearFilterValue.indexOf("family") != -1);

回答by Doorknob

containsis not supported in Chrome, but you could use a polyfill:

containsChrome 不支持,但您可以使用 polyfill:

if (!String.prototype.contains) {
    String.prototype.contains = function(s) {
        return this.indexOf(s) > -1
    }
}
'potato'.contains('tat') // true
'potato'.contains('tot') // false

回答by pala?н

Actually String.containsis not supported in Chrome as per the MDN

String.contains根据 Chrome实际上不支持MDN

Here's the way to resolve the issue:

以下是解决问题的方法:

Polyfill

填充物

You can easily polyfill this method :

您可以轻松地填充此方法:

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');
}

Demo: Fiddle

演示:小提琴

回答by Dinnu Buddy

You must understand why containsis not working. Actually there are two methods containsand includesand both have different usage.

您必须了解为什么contains不起作用。实际上有两种方法包含包含,两者都有不同的用法。

contains :

包含:

The contains() method returns a Boolean value indicating whether a node is a descendant of a specified node.

A descendant can be a child, grandchild, great-grandchild, and so on.

contains() 方法返回一个布尔值,指示节点是否是指定节点的后代。

后代可以是孩子、孙子、曾孙等。

var span = document.getElementById("mySPAN");
var div = document.getElementById("myDIV").contains(span);

The result of div will be:

div 的结果将是:

true

please find jsfiddle

请找到jsfiddle

includes :

包括:

Check if a string includes with "world":

检查字符串是否包含“world”:

var str = "Hello world, welcome to the universe.";
var n = str.includes("world");

The result of n will be:

n 的结果将是:

true

Definition and Usage

The includes() method determines whether a string contains the characters of a specified string.

This method returns true if the string contains the characters, and false if not.

Note: The includes() method is case sensitive.

定义和用法

include() 方法确定字符串是否包含指定字符串的字符。

如果字符串包含字符,则此方法返回 true,否则返回 false。

注意:includes() 方法区分大小写。

Please find jsfiddle

请找到jsfiddle

And finally ofcourse you can also use indexOf method to achive same output

最后当然你也可以使用 indexOf 方法来实现相同的输出

var ClearFilterValue = 'family Schools';
alert(ClearFilterValue.indexOf("family") != -1);

Hope this make sense

希望这是有道理的

回答by mithataydogmus

Use your own contains method and override firefox's contains() method. Because this method is widely used I mean -> (indexof).

使用您自己的 contains 方法并覆盖 firefox 的 contains() 方法。因为这种方法被广泛使用,所以我的意思是 -> ( indexof)。

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

回答by Robert Hoffmann

I think you should be using indexOf

我认为你应该使用 indexOf

var ClearFilterValue = 'family Schools';
if(ClearFilterValue.indexOf("family") !== -1) {
      alert('Success');
}

Also not sure if you should be making a polyfill as pretty much no browser seems to support it anyways https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/contains

也不确定您是否应该制作 polyfill,因为几乎没有浏览器似乎支持它https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/contains

None of IE's support it, Chrome apparently starting at 30, FF starting at 19, and i doubt mobile browsers support it either

没有一个 IE 支持它,Chrome 显然从 30 开始,FF 从 19 开始,我怀疑移动浏览器是否支持它