1. 程式人生 > >js中的"=="和equals()以及is()三者的區別

js中的"=="和equals()以及is()三者的區別

 一、

   在 javaScript或者jQuery中字串比較沒有equals()方法,要比較兩個字串是否相等可以直接用==或者is()進行判斷。

    例如:

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

    當然我們可以自己寫一個equals()方法:

    如:

    1.

Js程式碼  收藏程式碼
  1. String.prototype.equals = function(s){  
  2.     return this == s;  
  3. }   

    2.

Js程式碼  收藏程式碼
  1. function equals(str1, str2)    
  2. {    
  3.     if(str1 == str2)    
  4.     {    
  5.         return true;    
  6.     }    
  7.     return false;    
  8. }    

   二、

 is(expr)

  用一個表示式來檢查當前選擇的元素集合,如果其中至少有一個元素符合這個給定的表示式就返回true。   如果沒有元素符合,或者表示式無效,都返回'false'. 'filter' 內部實際也是在呼叫這個函式,所以,filter()函式原有的規則在這裡也適用。 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.

返回值

Boolean

引數

expr (String) :用於篩選的表示式

示例

由於input元素的父元素是一個表單元素,所以返回true。

HTML 程式碼:

Html程式碼  收藏程式碼
  1. <form><input type="checkbox" /></form>  

 jQuery 程式碼:

Js程式碼  收藏程式碼
  1. $("input[type='checkbox']").parent().is("form")  

結果:

  true Js程式碼  收藏程式碼
  1. <script language="javascript" src="jquery.js"
    ></script>     
  2. <script>     
  3. jQuery(function($){     
  4.     $(".abc").click(function(){     
  5.         if ($(this).next(".content").is(":hidden")) {     
  6.             $(this).next(".content").fadeIn("slow");     
  7.             $(this).html("收起");     
  8.             $(this).addClass("closeMore");     
  9.         } else {     
  10.             $(this).next(".content").fadeOut("slow");     
  11.             $(this).html("開啟");     
  12.             $(this).addClass("closeMore");           
  13.         }     
  14.     })     
  15. })     
  16. </script>    
  三、

  ==為js的比較執行符。

比較運算子

比較運算子在邏輯語句中使用,以測定變數或值是否相等。

給定 x=5,下面的表格解釋了比較運算子:

運算子 描述 例子
== 等於 x==8 為 false
=== 全等(值和型別) x===5 為 true;x==="5" 為 false
!= 不等於 x!=8 為 true
> 大於 x>8 為 false
< 小於 x<8 為 true
>= 大於或等於 x>=8 為 false
<= 小於或等於 x<=8 為 true

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

0==false// true0===false// false, because they are of a different type1=="1"// true, auto type coercion1==="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");