jQuery

[Traversing] JQ - slice() 메서드 - 특정 색인번호 범위 내로 요소 선택 범위 축소. (= slice메서드 = 슬라이스메서드)

목차

  1. slice() 예제 - 양수 (start <= index)
  2. slice() 정의 
  3. slice() 구문 
  4. slice() 예제 - 양수 (start <= index < stop)
  5. slice() 예제 - 음수 (start <= index)
  6. slice() 예제 - 음수 (start <= index < stop)

 

slice() 예제 - 양수 (start <= index)

 

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

<script>

$(document).ready(function(){

    $("p").slice(2).css("background-color", "yellow");

});

</script>


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

<p>index 0</p>

<p>index 1</p>

<p>index 2</p>

<p>index 3</p>

 

결과보기

 

slice() 정의 

 

선택자가 가리키는 요소 중, 특정 색인번호 범위 내 요소만 선택. 

 


 

1.

색인번호 범위: start <= index < stop

 

2.

stop 없는 경우, 마지막 요소까지 자동 선택. 

 

 

slice() 구문 

 

$(selector).slice(start,stop)

 


[매개변수]

 

start

필수. 시작 색인번호. (포함 O). (색인은 0부터 시작)

앞부터 색인 : 0부터 시작.

뒤부터 색인 : -1부터시작

 

stop

선택. 종료 색인번호. (포함 X) 

 


[반환값]

 

start <= index < stop 범위의 요소 선택. 

stop 없는 경우, 마지막 요소까지 자동 선택.

 

 

slice() 예제 - 양수 (start <= index < stop)

 

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

<script>

$(document).ready(function(){

    $("p").slice(2,4).css("background-color", "yellow");

});

</script>


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

<p>index 0</p>

<p>index 1</p>

<p>index 2</p>

<p>index 3</p>

<p>index 4</p>

<p>index 5</p>

 

결과보기


slice() 예제 - 음수 (start <= index)

 

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

<script>

$(document).ready(function(){

  $("p").slice(-3).css("background-color", "yellow");

});

</script>


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

<p>index 0</p>

<p>index 1</p>

<p>index 2</p>

<p>index 3</p>

<p>index 4</p>

<p>index 5</p>

 

결과보기

 

slice() 예제 - 음수 (start <= index < stop)

 

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

<script>

$(document).ready(function(){

  $("p").slice(-3,-1).css("background-color", "yellow");

});

</script>


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

<p>index 0</p>

<p>index 1</p>

<p>index 2</p>

<p>index 3</p>

<p>index 4</p>

<p>index 5</p>

 

결과보기



분류 제목
Traversing JQ - find() 메서드 ★★★ - 선택 요소의 자손요소 반환・찾기 (= find메서드 = 파인드메서드)
Traversing JQ - first() 메서드 - 선택요소의 첫번째요소 반환 (= first메서드 = 퍼스트메서드)
Traversing JQ - has() 메서드 - 내부에 하나 이상의 요소가 있는 모든 요소 반환 (= has메서드 = 해즈 메…
Traversing JQ - is() 메서드 ★ - 일치 여부 체크. (= is메서드 = 이즈메서드)
Traversing JQ - last() 메서드 - 선택요소들의 마지막 요소 반환
Traversing JQ - map() 메서드 - 일치하는 요소집합의 각 요소를 함수를 통해 전달하여 반환값 포함하는 새 jQu…
Traversing JQ - next() 메서드 - 선택요소 다음 형제요소 반환. (= next메서드 = 넥스트메서드)
Traversing JQ - nextAll() 메서드 - 선택요소의 모든 다음 형제요소 반환. (= nextAll메서드 = 넥스…
Traversing JQ - nextUntil() 메서드 - 주어진 두 인수 사이의 모든 다음 형제요소 반환
Traversing JQ - not() 메서드 - 일치 안 하는 요소 선택 (= not메서드 = 낫메서드)
Traversing JQ - offsetParent() 메서드 - 최초로 position된 부모요소 반환 (= offsetPar…
Traversing JQ - parent() 메서드 - 선택요소의 직접 부모요소 반환 (= parent메서드 = 패런트 메서드)
Traversing JQ - parents() 메서드 - 선택요소 모든 조상요소 반환 (= parents메서드 = 패런츠 매서드…
Traversing JQ - parentsUntil() 메서드 - 지정요소 사이의 조상요소 반환 (= parentsUntil메서…
Traversing JQ - prev() 메서드 - 선택요소 이전 형제요소 반환 (= prev메서드 = 프리브메서드)
2/3
목록
 홈  PC버전 로그인 일본어
그누앞단언어
그누뒷단언어
그외코딩언어
그누보드
제작의뢰
Q&A
커뮤니티 2
웹유틸
회원센터
홈짱닷컴 PC버전 로그인