Javascript 检测div在阵列中的位置

Javascript 检测div在阵列中的位置,javascript,jquery,html,jquery-selectors,Javascript,Jquery,Html,Jquery Selectors,我想知道一个div在一个参数中的位置:我的意思是,如果我有几个同名的div,比如: <div class="woot"></div> <div class="woot"></div> <div class="woot"></div> <div class="woot"></div> <div class="woot"></div> 我想检测一下div点击的位置:第一、第二

我想知道一个div在一个参数中的位置:我的意思是,如果我有几个同名的div,比如:

<div class="woot"></div>
<div class="woot"></div>
<div class="woot"></div>
<div class="woot"></div>
<div class="woot"></div>

我想检测一下div点击的位置:第一、第二……


<div class="parent">
   <div class="woot"></div>
   <div class="woot"></div>
   <div class="woot"></div>
   <div class="woot"></div>
   <div class="woot"></div>
</div>
<script type="text/javascript">

$(document).ready(function(){

   var woots = $(".parent").find(".woot");

   $(woots).each(function(index){
       $(this).attr("data-position", index);
   });

   $(".parent").on("click", ".woot", function(){
       console.log($(this).data("position"));
   });
});

</script>
$(文档).ready(函数(){ var woots=$(“.parent”).find(“.woot”); $(woots)。每个(函数(索引){ $(this).attr(“数据位置”,索引); }); $(“.parent”)。在(“单击“,”.woot“,函数()上){ console.log($(this.data(“position”)); }); });
例如,如果您在Chrome等浏览器中运行控制台,则应在控制台中看到该位置。


$(文档).ready(函数(){
var woots=$(“.parent”).find(“.woot”);
$(woots)。每个(函数(索引){
$(this).attr(“数据位置”,索引);
});
$(“.parent”)。在(“单击“,”.woot“,函数()上){
console.log($(this.data(“position”));
});
});
例如,如果您在Chrome等浏览器中运行控制台,则应在控制台中看到该位置。


$(文档).ready(函数(){
var woots=$(“.parent”).find(“.woot”);
$(woots)。每个(函数(索引){
$(this).attr(“数据位置”,索引);
});
$(“.parent”)。在(“单击“,”.woot“,函数()上){
console.log($(this.data(“position”));
});
});
例如,如果您在Chrome等浏览器中运行控制台,则应在控制台中看到该位置。


$(文档).ready(函数(){
var woots=$(“.parent”).find(“.woot”);
$(woots)。每个(函数(索引){
$(this).attr(“数据位置”,索引);
});
$(“.parent”)。在(“单击“,”.woot“,函数()上){
console.log($(this.data(“position”));
});
});
例如,如果在诸如Chrome之类的浏览器中运行,您应该可以在控制台中看到该位置。

使用index()函数,它将返回所选内容中单击元素的从零开始的索引:

$('.woot').click(function(){
    var clickedIndex = $('.woot').index($(this));
})
请参见此处:

使用index()函数,它将返回所选内容中单击元素的从零开始的索引:

$('.woot').click(function(){
    var clickedIndex = $('.woot').index($(this));
})
请参见此处:

使用index()函数,它将返回所选内容中单击元素的从零开始的索引:

$('.woot').click(function(){
    var clickedIndex = $('.woot').index($(this));
})
请参见此处:

使用index()函数,它将返回所选内容中单击元素的从零开始的索引:

$('.woot').click(function(){
    var clickedIndex = $('.woot').index($(this));
})

请看这里:

稍微过火,你不认为吗?与你的答案相比是的。稍微过火,你不认为吗?与你的答案相比是的。稍微过火,你不认为吗?与你的答案相比是的。稍微过火,你不认为吗?与你的答案相比是的。