Home > Web Front-end > JS Tutorial > body text

'==', equals() and is() methods in js

高洛峰
Release: 2016-12-16 09:32:03
Original
4521 people have browsed it

There is no equals() method for string comparison in javaScript or jQuery. To compare whether two strings are equal, you can directly use == or is() to judge.

For example:

"a"=="a"

$("#a").val().is("a")

Of course we can write an equals() method ourselves:

Such as :

Js code

String.prototype.equals = function(s){  
    return this == s;  
}
Copy after login

Js code

function equals(str1, str2)    
{    
    if(str1 == str2)    
    {    
        return true;    
    }    
    return false;    
}
Copy after login

is(expr)

Use an expression to check the currently selected set of elements, and return true if at least one element matches the given expression.

If no elements match, or the expression is invalid, 'false' is returned. 'filter' actually calls this function internally, so the original rules of the filter() function also apply here.

Checks the current selection against an expression and returns true, if at least one element of the selection fits the given expression.

If no element fits, or the expression is not valid, then the response will be 'false'. ' filter' is used internally, therefore all rules that apply there apply here, as well.

Return value

Boolean

Parameters

expr (String): Expression for filtering

Example

Due to input The element's parent element is a form element, so true is returned.

HTML code:

Html code

<form><input type="checkbox" /></form>
Copy after login

jQuery code:

Js code

$("input[type=&#39;checkbox&#39;]").parent().is("form")
Copy after login

Result:

true

Js code

<script language="javascript" src="jquery.js"></script>     
<script>     
jQuery(function($){     
    $(".abc").click(function(){     
        if ($(this).next(".content").is(":hidden")) {     
            $(this).next(".content").fadeIn("slow");     
            $(this).html("收起");     
            $(this).addClass("closeMore");     
        } else {     
            $(this).next(".content").fadeOut("slow");     
            $(this).html("打开");     
            $(this).addClass("closeMore");           
        }     
    })     
})     
    
</script>
Copy after login

== j Comparison operator for s.

Comparison Operators

Comparison operators are used in logical statements to determine whether variables or values ​​are equal.

Given x=5, the following table explains the comparison operators:


Operator Description Example

== Equal to x==8 is false

=== Congruent (value and type ) x===5 is true;

>= Greater than or equal to x>=8 is false

<= Less than or equal to x<=8 is true

The 3 equal mean "equality without type coercion". Using the triple equal signs s , the values ​​must be equal in type as well.

0==false   // true
0===false  // false, because they are of a different type
1=="1"     // true, auto type coercion
1==="1"    // false, because they are of a different type
Copy after login

=== and !== are strict comparison operators:

JavaScript has both strict and type-converting equality comparison. For strict equality the objects being compared must have the same type and:
Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.
Two Boolean operands are strictly equal if both are true or both are false.
Two objects are strictly equal if they refer to the same Object.
Null and Undefined types are == (but not ===).
另:
 
1.document.getElementById
document.getElementsByName()
document.getElementsByTagName()
注意上面的Element后在Id中是没有加“s”的,特别容易写错.
 
2.注意属性选择器的使用
jQuery(&#39;[attribute="value"]&#39;)
$(&#39;input[value="Hot Fuzz"]&#39;).next().text(" Hot Fuzz");
Copy after login

More "==" and equals() in js and is () method related articles please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template