<div class="nav">
<a href="#item1">item1</a>
<a href="#item2">item2</a>
<a href="#item3">item3</a>
</div>
我有html結構如上,我想通過jquery獲得點擊超鏈接的索引。如何獲得超鏈接索引裏面div
感謝
<div class="nav">
<a href="#item1">item1</a>
<a href="#item2">item2</a>
<a href="#item3">item3</a>
</div>
我有html結構如上,我想通過jquery獲得點擊超鏈接的索引。如何獲得超鏈接索引裏面div
感謝
$(".nav a").click(function() {
console.log($(this).index());
});
這會給你的超鏈接的索引。
對於jQuery 1.7+,你可以使用on
,你可以看到這個像一個代理添加到超鏈接。
$(".nav").on("click", "a", function() {
console.log($(this).index());
});
您可以使用index()
$('.nav a').on('click', function() {
console.log($(this).index());
});
你可以試試這個,index()叫上元素的集合,DOM元素或jQuery對象傳遞中,的.index()返回一個整數,指示傳遞的元素相對於原始集合的位置。
$(function(){
$(".nav > a").click(function (event) {
console.log("Clicked link index::"+ $(this).index());
event.preventDefault();
});
});
你有沒有試過? –
有趣的如何1分鐘後回答在一分鐘內產生5個其他答案。 – Abbas
我曾試過, $(「。nav a」)。size();和$(「。nav a」)。length(); 但沒有工作, – vijay