jQuery
is()로 요소 검사하기
hjcode
2019. 9. 26. 16:32
selector, element 에 대해 일치하는 요소를 검사하고 그 중 하나 이상이 일치하면 true를 반환합니다.
<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>
$("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");
}); // 두번째 li를 초록색으로
jsfiddle에서 보기
반응형