.first()

· 2017-09-26 (화) 22:43:52 · 1801

.first()


설명 : 일치하는 요소 집합을 집합의 첫 번째 집합으로 줄입니다.


DOM 요소 집합을 나타내는 jQuery 객체가 주어지면이 .first()메서드는 해당 집합의 첫 번째 요소에서 새 jQuery 객체를 생성합니다.


간단한 목록이있는 페이지를 고려해보십시오.


<ul>

  <li>list item 1</li>

  <li>list item 2</li>

  <li>list item 3</li>

  <li>list item 4</li>

  <li>list item 5</li>

</ul>

이 메소드를 목록 항목 집합에 적용 할 수 있습니다.


$( "li" ).first().css( "background-color", "red" );

이 호출의 결과는 첫 번째 항목에 대한 빨간색 배경입니다.


예:

단락의 첫 번째 범위를 강조 표시하십시오.


<!doctype html>

<html lang="en">

<head>

  <meta charset="utf-8">

  <title>first demo</title>

  <style>

  .highlight{

    background-color: yellow

  }

  </style>

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

</head>

<body>

 

<p>

  <span>Look:</span>

  <span>This is some text in a paragraph.</span>

  <span>This is a note about it.</span>

</p>

 

<script>

$( "p span" ).first().addClass( "highlight" );

</script>

 

</body>

</html>

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

개발자팁

5,403건

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

+
분류 제목 글쓴이 날짜 조회
jQuery 17-10-11 조회 2,429
jQuery 17-10-11 조회 1,897
jQuery 17-10-11 조회 2,010
jQuery 17-09-29 조회 2,236
jQuery 17-09-29 조회 2,481
jQuery 17-09-29 조회 2,021
jQuery 17-09-26 조회 1,941
jQuery 17-09-26 조회 2,029
jQuery
[jQuery]
17-09-26 조회 1,802
jQuery 17-09-22 조회 2,046
jQuery 17-09-22 조회 2,104
jQuery 17-09-22 조회 2,092
jQuery 17-09-21 조회 2,268
jQuery 17-09-21 조회 2,219
jQuery 17-09-21 조회 2,087
jQuery 17-09-19 조회 2,142
jQuery 17-09-19 조회 1,899
jQuery
[jQuery]
17-09-19 조회 1,882
jQuery
[jQuery]
17-09-18 조회 2,067
jQuery 17-09-18 조회 1,982
jQuery 17-09-18 조회 1,671
jQuery 17-09-14 조회 2,082
jQuery 17-09-14 조회 1,778
jQuery 17-09-14 조회 2,025
jQuery 17-09-11 조회 2,419
jQuery 17-09-11 조회 2,443
jQuery 17-09-11 조회 1,967
jQuery 17-09-10 조회 2,033
jQuery 17-09-10 조회 2,456
jQuery
[jQuery]
17-09-10 조회 1,916