ホームページ  >  記事  >  ウェブフロントエンド  >  js の「==」、equals() および is() メソッド

js の「==」、equals() および is() メソッド

高洛峰
高洛峰オリジナル
2016-12-16 09:32:034468ブラウズ

JavaScriptやjQueryには文字列比較のequals()メソッドがありません。2つの文字列が等しいかどうかを比較するには、==またはis()を直接使用して判断できます。

例:

"a"=="a"

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

もちろん、equals() メソッドを自分で書くこともできます:

例:

Js コード

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

Js コード

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

is(expr)

式を使用して、現在選択されている要素のセットを確認し、少なくとも 1 つの要素が指定された式に一致する場合に true を返します。

一致する要素がない場合、または式が無効な場合、「filter」は実際にこの関数を内部で呼び出すため、filter() 関数の元の規則もここに適用されます。

現在の選択内容を式と照合し、選択内容の少なくとも 1 つの要素が指定された式に一致する場合は true を返します。

要素が一致しない場合、または式が無効な場合、応答は「false」になります。 filter' は内部で使用されるため、そこで適用されるすべてのルールがここにも適用されます。

戻り値

ブール

パラメータ

expr (文字列): フィルタリングの式

入力による要素の親要素はform要素なのでtrueが返されます。

HTML コード:

HTML コード

<form><input type="checkbox" /></form>

jQuery コード:

Js コード

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

結果:

true

Js コード

<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 s.

比較演算子

比較演算子は、変数または値が等しいかどうかを判断するために論理ステートメントで使用されます。

x=5 の場合、次の表で比較演算子を説明します。


演算子 説明 例

== x==8 に等しい場合は false

=== 合同 (値と型) x= ==5 は true

>= x 以上>=8 は false

<= x

3 つの等しいは「等しい」を意味します。型強制なし"。三重等号 s を使用すると、値の型も同じである必要があります。

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(&#39;[attribute="value"]&#39;)
$(&#39;input[value="Hot Fuzz"]&#39;).next().text(" Hot Fuzz");

More " jsの==とequals()、is()メソッド関連の記事はPHPの中国語サイトに注目してください!


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:jQuery関数次の記事:jQuery関数