js의 '==', equals() 및 is() 메소드

高洛峰
풀어 주다: 2016-12-16 09:32:03
원래의
4467명이 탐색했습니다.

javaScript나 jQuery에는 문자열 비교를 위한 equals() 메서드가 없습니다. 두 문자열이 같은지 비교하려면 == 또는 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)

표현식을 사용하여 현재 선택된 요소 집합을 확인하고, 하나 이상의 요소가 주어진 표현식과 일치하면 true를 반환합니다.

일치하는 요소가 없거나 표현식이 유효하지 않으면 'false'가 반환됩니다. 'filter'는 실제로 이 함수를 내부적으로 호출하므로 여기에도 filter() 함수의 원래 규칙이 적용됩니다.

현재 선택 항목을 표현식과 비교하여 확인하고 선택 요소 중 하나 이상이 주어진 표현식과 일치하면 true를 반환합니다.

맞는 요소가 없거나 표현식이 유효하지 않으면 응답은 'false'입니다. 'filter'는 내부적으로 사용되므로 여기에 적용되는 모든 규칙이 여기에도 적용됩니다. >

expr(String): 필터링에 사용되는 표현식

예제

입력 요소의 상위 요소가 폼 요소이므로 true를 반환합니다.

HTML 코드:

Html 코드

jQuery 코드:

Js 코드

결과:
<form><input type="checkbox" /></form>
로그인 후 복사

true

$("input[type=&#39;checkbox&#39;]").parent().is("form")
로그인 후 복사
Js 코드

==는 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>
로그인 후 복사
비교 연산자는 논리문에서 변수나 값이 같은지 여부를 확인하는 데 사용됩니다.

x=5일 때 다음 표에서는 비교 연산자를 설명합니다.

연산자 설명 예

= = x==8과 같음은 false

=== 합동(값 및 유형) x===5는 true입니다.

!= 아님 x!=8은 true

> x>8보다 큼은 false

< x<8보다 작음은 true

>=보다 큼 x>=8과 같음은 거짓

<=이 x<=8보다 작거나 같음은 참

3이 같음 기호는 "형 강제가 없는 동일성"을 의미합니다. 삼중 등호를 사용하면 값의 유형도 동일해야 합니다.

=== 및 !==는 엄격한 비교 연산자입니다.

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");
로그인 후 복사
js의 "==", equals() 및 is() 메소드와 관련된 더 많은 기사를 보려면 다음을 참고하세요. PHP 중국어 웹사이트!


관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!