.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에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4796 | jQuery | 8년 전 | 1539 | ||
| 4795 | jQuery | 8년 전 | 1442 | ||
| 4794 | jQuery | 8년 전 | 1568 | ||
| 4793 | jQuery | 8년 전 | 1621 | ||
| 4792 | jQuery | 8년 전 | 1638 | ||
| 4791 | jQuery | 8년 전 | 1797 | ||
| 4790 | jQuery | 8년 전 | 1737 | ||
| 4789 | jQuery | 8년 전 | 1600 | ||
| 4788 | jQuery | 8년 전 | 1661 | ||
| 4787 | jQuery | 8년 전 | 1476 | ||
| 4786 | jQuery | 8년 전 | 1435 | ||
| 4785 | jQuery | 8년 전 | 1648 | ||
| 4784 | jQuery | 8년 전 | 1532 | ||
| 4783 | jQuery | 8년 전 | 1235 | ||
| 4782 | jQuery | 8년 전 | 1605 | ||
| 4781 | jQuery | 8년 전 | 1344 | ||
| 4780 | jQuery | 8년 전 | 1578 | ||
| 4779 | jQuery | 8년 전 | 1988 | ||
| 4778 | jQuery | 8년 전 | 1963 | ||
| 4777 | jQuery | 8년 전 | 1535 | ||
| 4776 | jQuery | 8년 전 | 1589 | ||
| 4775 | jQuery | 8년 전 | 1974 | ||
| 4774 | jQuery | 8년 전 | 1491 | ||
| 4773 | jQuery | 8년 전 | 1416 | ||
| 4772 | jQuery | 8년 전 | 2206 | ||
| 4771 | jQuery | 8년 전 | 3621 | ||
| 4770 | PHP | 8년 전 | 5585 | ||
| 4769 | 기타 | 8년 전 | 1967 | ||
| 4768 | 기타 | 8년 전 | 1785 | ||
| 4767 | 기타 | 8년 전 | 2073 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기