0
$(document).ready(function() {
var box = $('.box');
box.click(function() {
console.log($(this).index());
});
});
div, section {
border: 1px solid red;
width: 80px;
height: 80px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box">1</div>
<div class="box">2</div>
<div class="para">3</div>
<div class="box">4</div>
在上述HTML,与.box
类的最后一个div是第三.box
,我想最后.box
的索引是3
,但我得到4
。我应该使用哪个jQuery函数,它将搜索所有div类的box
,然后在点击.box
的位置返回我的位置.box
?
索引返回被考虑到母体内的所有元素。如果你想过滤你可以为方法提供一个选择器或者在选择器上调用它。 [jQuery .index()](https://api.jquery.com/index/) – melancia
http://api.jquery.com/index它的记录很好。 –