JQuery

[Selector] JQ - :last-child 가상선택자 - 마지막 자식요소 선택. (= 라스트 차일드 선택자)

목차
  1. :last-child 예제 - 그 부모의 마지막 자식인 p 요소
  2. :last-child 정의
  3. :last-child 구문
  4. :last-child 예제 - 모든 div의 마지막 p 요소 선택
  5. :last-child 예제 - cf. :last 선택자
  6. :last-child 예제 - cf. :last-of-type 선택자

 

:last-child 예제 - 그 부모의 마지막 자식인 p 요소

 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<script>

$(document).ready(function(){

  $("p:last-child").css("background-color", "yellow");

});

</script>


<p>홈짱닷컴 Homzzang.com</p>


<div style="border:1px solid;">

  <p>HTML</p>

  <p>구조</p>

</div><br>


<div style="border:1px solid;">

  <p>CSS</p>

  <p>디자인</p>

  <span>구조보충</span>

</div>


<p>홈페이지 제작관리 강의</p>

 

결과보기

 

:last-child 정의

 

그 부모의 마지막 자식 요소인 모든 요소 선택.

 


 

1.

다른 선택자와 결합해 마지막 자식 한정.

만약, 마지막 자식이 아니면, 해당 안 함. (위 예제.)

 

2. cf 함수.

  • :first-child 선택자 : 그 부모의 첫 번째 자식 요소 모두 선택.
  • :last 선택자 : 마지막 선택자.
  • :last-of-type 선택자 : 그 부모의 마지막 자식 요소 유형 모두 선택.

 

 

:last-child 구문

 

$(":last-child")

 

 

:last-child 예제 - 모든 div의 마지막 p 요소 선택

 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<script>

$(document).ready(function(){

  $("div p:last-child").css("background-color", "yellow");

});

</script>


<p>홈짱닷컴 Homzzang.com</p>


<div style="border:1px solid;">

  <p>HTML</p>

  <p>구조</p>

</div><br>


<div style="border:1px solid;">

  <p>CSS</p>

  <p>디자인</p>

  <span>구조보충</span>

</div>


<p>홈페이지 제작관리 강의</p>


결과보기

 

:last-child 예제 - cf. :last 선택자

※ :last 선택자 : 맨 마지막 요소만 선택.

 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<script>

$(document).ready(function(){

  $("#btn1").click(function(){

    $("p:last").css("background-color", "red");

  });

  $("#btn2").click(function(){

    $("p:last-child").css("background-color", "yellow");

  });

});

</script>


<button id="btn1">:last</button>

<button id="btn2">:last-child</button><br><br>


<div style="border:1px solid">

  <p>홈짱닷컴/p>

  <p>Homzzang.com</p>

</div><br>


<div style="border:1px solid">

  <p>홈페이지 제작관리 강의</p>

  <p>코딩언어 그누보드 강의</p>

</div>

 

결과보기

 

:last-child 예제 - cf. :last-of-type 선택자

 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<script>

$(document).ready(function(){

  $("button").click(function(){

    var btn = $(this).text();

    $("p").css("background-color", "white"); 

    $("p" + btn).css("background-color", "yellow"); 

  });

});

</script>


<button>:last</button>

<button>:last-child</button>

<button>:last-of-type</button><br><br>


<p>홈짱닷컴</p>


<div style="border:1px solid;">

  <p>HTML</p>

  <p>구조</p>

</div><br>


<div style="border:1px solid;">

  <span>CSS</span>

  <p>디자인</p>

  <p>구조 보충</p>

  <span>무료</span>

</div><br>


<div style="border:1px solid">

  <p>JS</p>

  <p>기능</p>

</div>


<p>홈페이지 제작관리 강의</p>

 

결과보기


방문 감사합니다. (즐겨찾기 등록: Ctrl + D)

분류 제목
Traversing JQ - Traversing - 특정요소찾기 (= 특정요소선택 = 트래버싱)
Traversing JQ - parent() , parents() , parentsUntil() 메서드 - 부모요소찾기 (= 부…
Traversing JQ - children() 메서드 - 자식요소찾기 / find() 메서드 - 자손요소찾기
Traversing JQ - 형제찾기 메서드 - siblings() , next() , nextAll() , nextUntil(…
Traversing JQ - 검색필터링 메서드 - first() , last() , eq() , filter() , not() …
AJAX JQ - AJAX (아작스) 소개 - 정의 / 기본예제 / 메서드종류
AJAX JQ - load() 메서드 ★ - 파일 내용 가져오기 (= load메서드 = 아작스 로드메서드)
AJAX JQ - get()/post() 메서드 - 서버에 정보요청 (= get메서드 = 겟메서드 / post메서드 …
Misc JQ - noConflict() 메서드 ★★★ - javascript (JS) 기반의 다른 프레임워크와의 충…
Examples jQuery Examples - 제이쿼리 예제 복습
Examples JQ - Quiz - 제이쿼리 퀴즈테스트
Selector JQ - * 선택자 - 모든 요소 선택. (= 전체선택자 = 전체 요소선택자 = 아스테리크 선택자)
Selector JQ - #id 선택자 - 지정 아이디 갖는 요소 선택. (= 아이디 선택자)
Selector JQ - .class 선택자 - 지정 클래스 갖는 요소 선택. (= 클래스 선택자)
Selector JQ - element 선택자 - 지정 요소 모두 선택. (= 요소선택자)
3/20
목록
  • 채팅방
  • 필독
1. 채팅창 헤드에서 접속자 확인 2. 닉네임 클릭해 1:1 채팅 가능 3. 닉네임 클릭해 귓속말 가능 4. 닉네임 클릭해 호출하기 가능 5. 우하단 클릭해 환경 설정 가능 6. 의뢰글 작성 후 의뢰 상담 가능 7. 질문글 작성 후 질문 상담 가능 8. 채팅방에 개인정보 입력 금지 9. 채팅방에 광고 욕설 비방 금지
 홈  PC버전 로그인 일본어
웹디자인언어
서버관리언어
고급코딩언어
그누보드
제작의뢰
Q&A
커뮤니티 1
웹유틸
회원센터
홈짱 PC버전 로그인