.has (selector)
.has (selector)
설명 : 일치하는 요소 집합을 선택기 또는 DOM 요소와 일치하는 하위 항목이있는 요소로 줄입니다.
DOM 요소 집합을 나타내는 jQuery 객체가 주어지면이 .has()메소드는 일치하는 요소의 하위 집합에서 새 jQuery 객체를 생성합니다. 제공된 선택기는 일치하는 요소의 하위 항목에 대해 테스트됩니다. 하위 요소 중 하나가 선택자와 일치하면 요소가 결과에 포함됩니다.
다음과 같이 중첩 목록이있는 페이지를 고려하십시오.
<ul>
<li>list item 1</li>
<li>list item 2
<ul>
<li>list item 2-a</li>
<li>list item 2-b</li>
</ul>
</li>
<li>list item 3</li>
<li>list item 4</li>
</ul>
이 메소드를 다음과 같이 목록 항목 집합에 적용 할 수 있습니다.
$( "li" ).has( "ul" ).css( "background-color", "red" );
이 호출의 결과는 항목 2의 빨간색 배경입니다 . 하위 항목 중 하나만 <li>있는 항목이기 때문입니다 <ul>.
예:
요소가 다른 요소 안에 있는지 확인하십시오.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>has demo</title>
<style>
.full {
border: 1px solid red;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<ul><li>Does the UL contain an LI?</li></ul>
<script>
$( "ul" ).append( "<li>" +
( $( "ul" ).has( "li" ).length ? "Yes" : "No" ) +
"</li>" );
$( "ul" ).has( "li" ).addClass( "full" );
</script>
</body>
</html>
게시판 목록
개발자팁
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4736 | jQuery | 8년 전 | 1561 | ||
| 4735 | jQuery | 8년 전 | 1748 | ||
| 4734 | jQuery | 8년 전 | 1739 | ||
| 4733 | jQuery | 8년 전 | 2036 | ||
| 4732 | jQuery | 8년 전 | 1958 | ||
| 4731 | jQuery | 8년 전 | 1460 | ||
| 4730 | jQuery | 8년 전 | 1918 | ||
| 4729 | PHP |
|
8년 전 | 6904 | |
| 4728 | jQuery | 8년 전 | 2563 | ||
| 4727 | jQuery | 8년 전 | 1611 | ||
| 4726 | jQuery | 8년 전 | 1691 | ||
| 4725 | jQuery | 8년 전 | 1835 | ||
| 4724 | jQuery | 8년 전 | 1312 | ||
| 4723 | jQuery | 8년 전 | 1864 | ||
| 4722 | jQuery | 8년 전 | 1628 | ||
| 4721 | jQuery | 8년 전 | 1918 | ||
| 4720 | jQuery | 8년 전 | 1977 | ||
| 4719 | jQuery | 8년 전 | 1603 | ||
| 4718 | jQuery | 8년 전 | 1608 | ||
| 4717 | jQuery | 8년 전 | 1559 | ||
| 4716 | jQuery | 8년 전 | 1237 | ||
| 4715 | jQuery | 8년 전 | 1395 | ||
| 4714 | jQuery | 8년 전 | 1417 | ||
| 4713 | jQuery | 8년 전 | 1335 | ||
| 4712 | jQuery | 8년 전 | 2228 | ||
| 4711 | jQuery | 8년 전 | 1713 | ||
| 4710 | PHP |
|
8년 전 | 3748 | |
| 4709 | jQuery | 8년 전 | 1630 | ||
| 4708 | jQuery | 8년 전 | 1910 | ||
| 4707 | jQuery | 8년 전 | 1764 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기