首页 > 解决方案 > 点击时 - 找到被点击元素的索引 - 它之前出现了多少次?

问题描述

$(document).on('click', '.productPhoto', function(){
  console.log($(this).prevAll(".productPhoto").length); // * this is photo number 3
});

我需要检查点击之前有多少productPhoto元素(=它的索引)

这不起作用,因为prevAll是针对兄弟姐妹productPhoto的,我需要搜索在单击的容器(不是兄弟姐妹)下出现了多少次。


那么-在名为的容器中,单击products的索引是多少?productPhoto

标签: jquery

解决方案


嗯,它很简单 console.log( $( ".productPhoto" ).index( this ) );


推荐阅读