Posts /

is()로 요소 검사

Twitter Facebook Google+
18 Feb 2019

is()를 이용해 확인하기

selector, element 에 대해 일치하는 요소를 검사하고 그 중 하나 이상이 일치하면 true를 반환합니다.

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>
js
$("li").click(function() {
  var $li = $(this);
  var isWithTwo = $li.is(function() {
    return $('strong', this).length===2;
  });

  if (isWithTwo)
    $li.css("background-color", "green");
  else
    $li.css("background-color", "red");
});
결과

jsfiddle에서 보기


Twitter Facebook Google+