수

.has (selector)

· 2017-10-12 (목) 16:36:50 · 1723

.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>

|
댓글을 작성하시려면 로그인이 필요합니다.

개발자팁

5,403건

개발과 관련된 유용한 정보를 공유하세요. 질문은 QA에서 해주시기 바랍니다.

+
분류 제목 글쓴이 날짜 조회
jQuery 17-11-24 조회 2,595
jQuery 17-11-24 조회 2,713
jQuery 17-11-21 조회 2,934
jQuery 17-11-21 조회 3,157
jQuery 17-11-21 조회 2,370
jQuery 17-11-10 조회 3,366
jQuery 17-11-10 조회 2,829
jQuery
[jQuery]
17-11-10 조회 2,167
JavaScript 17-11-09 조회 3,259
jQuery
[jQuery]
17-11-08 조회 1,984
jQuery 17-11-08 조회 2,260
jQuery 17-11-08 조회 2,345
jQuery 17-10-24 조회 2,130
jQuery 17-10-24 조회 2,200
jQuery
[jQuery]
17-10-24 조회 1,899
jQuery 17-10-20 조회 2,501
jQuery 17-10-20 조회 2,938
jQuery
[jQuery]
17-10-20 조회 2,227
jQuery 17-10-19 조회 1,928
jQuery
[jQuery]
17-10-19 조회 2,347
jQuery 17-10-19 조회 5,951
기타 17-10-19 조회 3,674
jQuery
[jQuery]
17-10-17 조회 2,078
jQuery 17-10-17 조회 2,235
jQuery 17-10-17 조회 2,321
PHP 17-10-16 조회 5,632
node.js 17-10-13 조회 4,671
jQuery 17-10-12 조회 2,727
jQuery 17-10-12 조회 2,161
jQuery 17-10-12 조회 1,724