문제

마지막 목록 항목의 인덱스 번호를 찾으려고 노력하고 있지만 내가 사용하는 jQuery는 계속해서 -1을 반환합니다. 이것은 내가 사용하는 JS와 HTML입니다.

var index = $('#imageThumbnails li:last').index(this);

<div id="imageThumbnails">
   <ul class="gallery_demo_unstyled">
      <li class="active"><img src="test-img.jpg" width="394" height="394" alt=" " /></li>
      <li><img src="test-img2.jpg" width="394" height="394" alt=" " /></li>
      <li><img src="test-img3.jpg" width="394" height="394" alt=" " /></li>
      <li><img src="test-img4.jpg" width="394" height="394" alt=" " /></li>
      <li><img src="test-img5.jpg" width="394" height="394" alt=" " /></li>
      <li><img src="test-img6.jpg" width="394" height="394" alt=" " /></li>
      <li><img src="test-img7.jpg" width="394" height="394" alt=" " /></li>
    </ul>
 </div>

당신의 도움을 주셔서 감사합니다.

도움이 되었습니까?

해결책

해당 컬렉션의 하위 항목을 통과하여 컬렉션에서 인덱스를 호출해야합니다.

var items = $('#imageThumbnails li');
var lastItem = $('#imageThumbnails li:last');
var index = items.index(lastItem);

클릭 기능 핸들러에있는 경우 다음과 같은 작업을 수행 할 수 있습니다.

var items = $('#imageThumbnails li').click(function() {
    var index = items.index(this);

    // now that I know where I am, why am I here?
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top