如何找到第二近的 div?
例如,为了获得最近的一个,我成功地使用了:
var closest_div_id = $(this).closest('div').attr('id');
现在,我如何获得第二近的?像这样的东西:
$(this).(closest('div').closest('div')).attr('id'); ???
最佳答案
假设this
不是 <div>
元素,您可以将选择器传递给 parents()并写下:
var secondClosestId = $(this).parents("div").eq(1).attr("id");
parents()
返回从最近的祖先排序的祖先,所以 eq()
可以在这里使用。参见 this question对于一般情况。
关于javascript - jQuery:寻找第二个最接近的div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11420697/