why javascript contains property is not working in chrome browser?

前端 未结 6 1302
孤城傲影
孤城傲影 2021-02-01 15:39

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

6条回答
  •  梦如初夏
    2021-02-01 16:07

    You must understand why contains is not working. Actually there are two methods contains and includes and both have different usage.

    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.

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

    The result of div will be:

    true
    

    please find jsfiddle

    includes :

    Check if a string includes with "world":

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

    The result of n will be:

    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.

    Please find jsfiddle

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

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

    Hope this make sense

提交回复
热议问题