在 javaScript或jQuery中字串比較沒有equals()方法,要比較兩個字串是否相等可以直接用==或is()進行判斷。
例如:
"a"=="a"
$("#a").val().is("a")
:
Js程式碼
String.prototype.equals = function(s){ return this == s; }
Js程式碼
function equals(str1, str2) { if(str1 == str2) { return true; } return false; }
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 valse filter' is used internally, therefore all rules that apply there apply here, as well.
回傳值
Boolean
參數
r: () 結論由於篩選式元素的父元素是表單元素,所以回傳true。
HTML 代碼:
Html代碼
<form><input type="checkbox" /></form>
jQuery 代碼:
Js代碼
$("input[type='checkbox']").parent().is("form")
==為js的比較運行符。 比較運算子比較運算子在邏輯語句中使用,以測定變數或數值是否相等。 給定x=5,以下的表格解釋了比較運算子:給定x=5,下面的表格解釋了比較運算子:給定x=5,下面的表格解釋了比較運算子:給定x== ) x===5 為true;x==="5" 為false != 不等於 x!=8 為true
x
>= 大於或等於 x>=8 為false
🎢 cion". Using the triple equals, the values must be equal in type as well.
<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>=== 和 !== are strict comparison operators:
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
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");文章請關注PHP中文網!