Home > Article > Web Front-end > "==", equals() and is() methods in js
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; }
Js code
function equals(str1, str2) { if(str1 == str2) { return true; } return false; }
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>
jQuery code:
Js code
$("input[type='checkbox']").parent().is("form")
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>
== 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
=== 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('[attribute="value"]') $('input[value="Hot Fuzz"]').next().text(" Hot Fuzz");
More "==" and equals() in js and is () method related articles please pay attention to the PHP Chinese website!