我正在尝试根据使用 eq()
获得的元素触发点击事件。
我有三个具有相同类名的元素:ag-list-item
。
<!-- item one -->
<div ag-repeat="" class="ag-list-item ag-not-dragging">
<span>
<span class="ag-visible-icons">
</span>
<span>Avg. Yield (bu/ac)</span>
</span>
</div>
<!-- /item one -->
<!-- item two -->
<div ag-repeat="" class="ag-list-item ag-not-dragging">
<span>
<span class="ag-visible-icons">
</span>
<span>Total Average</span>
</span>
</div>
<!-- /item two -->
<!-- item three -->
<div ag-repeat="" class="ag-list-item ag-not-dragging">
<span>
<span class="ag-visible-icons">
</span>
<span>Crop Type</span>
</span>
</div>
<!-- /item three -->
我试图在特定 ag-list-item
元素的嵌套 span 元素上触发点击事件。我使用 :first
来点击第一个,但我想定位到一个特定的索引(即第一、第二、第三等)
// click all with .ag-list-item class
$('.ag-list-item span span').click()
// click first item in set
$('.ag-list-item:first span span').click()
// try to only click the second item in the set
// get second
var el = $('.ag-list-item').children('span span').eq(2);
// click second item in set
$(el).click();
请帮忙!
最佳答案
.eq(2)
将为您提供第三项,因为此(以及大多数计算机编程世界)中的索引是从零开始的。您可能需要 .eq(1)
。
此外,.eq()
的目的是避免需要再次将结果重新包装在 jQuery 对象中。
我猜,看看这个,您正在寻找“第二个”列表项,并且您希望其中的所有跨度都获得点击处理程序?
// get second
var $el = $('.ag-list-item').eq(1);
// click second item in set
$el.children('span span').click();
关于javascript - 基于eq()选择的点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32483497/