본문으로 바로가기

[jQuery] .is()

category Helloworld!/Javascript & jQuery 2015. 10. 2. 14:00

.is()


is 매서드는 셀랙터의 대상을 다시 셀렉터로 비교하여 맞으면 true, 아니면 false를 반환한다.

예를들면 다음과 같다


- HTML
<ul>
  <li><strong>list</strong> item 1 - one strong tag</li>
  <li><strong>list</strong> item <strong>2</strong> -
    two <span>strong tags</span></li>
  <li>list item 3</li>
  <li>list item 4</li>
  <li>list item 5</li>
</ul>

- jquery
$( "li" ).click(function() {
  var li = $( this ),
    isWithTwo = li.is(function() {
      return $( "strong", this ).length === 2;
    });
  if ( isWithTwo ) {
    li.css( "background-color", "green" );
  } else {
    li.css( "background-color", "red" );
  }
});


참고사이트 : http://api.jquery.com/is/