jQuery ( ": first-child")

· 2017-09-26 (화) 22:44:51 · 2043 · 1

jQuery ( ": first-child")


설명 : 부모의 첫 번째 하위 요소 인 모든 요소를 ​​선택합니다.


반면 : 첫 번째 요소는 하나만 일치 하지만 :first-childselector는 둘 이상 (하나의 부모에 대해 하나)과 일치 할 수 있습니다. 이것은에 해당합니다 :nth-child(1).


예:

일치하는 div의 첫 번째 범위를 찾아서 밑줄을 긋고 호버 상태를 추가합니다.


<!doctype html>

<html lang="en">

<head>

  <meta charset="utf-8">

  <title>first-child demo</title>

  <style>

  span {

    color: #008;

  }

  span.sogreen {

    color: green;

    font-weight: bolder;

  }

  </style>

  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>

</head>

<body>

 

<div>

  <span>John,</span>

  <span>Karl,</span>

  <span>Brandon</span>

</div>

<div>

  <span>Glen,</span>

  <span>Tane,</span>

  <span>Ralph</span>

</div>

 

<script>

$( "div span:first-child" )

  .css( "text-decoration", "underline" )

  .hover(function() {

    $( this ).addClass( "sogreen" );

  }, function() {

    $( this ).removeClass( "sogreen" );

  });

</script>

 

</body>

</html>

|

댓글 1개

스크립트 nth-child 쓰기
댓글을 작성하시려면 로그인이 필요합니다.

개발자팁

5,403건

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

+
분류 제목 글쓴이 날짜 조회
node.js 19-01-11 조회 2,851
node.js 19-01-10 조회 2,583
node.js 19-01-10 조회 2,679
node.js 19-01-10 조회 2,396
node.js 19-01-09 조회 2,629
node.js 19-01-09 조회 2,766
node.js 19-01-09 조회 3,007
웹서버 19-01-04 조회 3,517
PHP 18-12-18 조회 2,839
웹서버 18-12-07 조회 2,441
JavaScript 18-12-06 조회 3,193
node.js 18-11-29 조회 4,305
기타 18-11-20 조회 4,849
기타 18-11-19 조회 2,860
기타 18-11-19 조회 2,572
기타 18-11-19 조회 2,619
Mobile 18-11-09 조회 2,847
JavaScript 18-11-08 조회 2,854
기타 18-11-08 조회 2,863
jQuery 18-11-06 조회 3,100
PHP 18-10-31 조회 5,885
jQuery 18-10-29 조회 5,760
기타 18-10-12 조회 3,349
MySQL 18-10-11 조회 4,733
기타 18-10-11 조회 2,826
웹서버 18-10-07 조회 3,043
MySQL 18-10-04 조회 2,828
MySQL 18-10-02 조회 3,304
JavaScript 18-10-02 조회 1.1만
웹서버 18-09-29 조회 2,969