簡體   English   中英

如何使用jquery從數組中查找單擊元素的索引?

[英]How to find the index of a clicked element from an array with jquery?

如何使用jquery ???從數組中找到單擊的錨標記的索引?

我想搜索是否存在eqqal單擊元素的元素,如果為true則返回該元素的索引。

我試過這樣的東西,但它以-1返回

$('#id').click(function(){

var obj = $('a').get(0).href;
var arr = $.makeArray(obj);
var getclickedhref = $(this).get(0).href;

var clickedindex = $.inArray(getclickedhref, arr);

console.log(clickedindex);
});

請你能幫幫我嗎??!

我不確定所有getmakeArray內容是什么,但我認為你正在尋找index

從匹配的元素中搜索給定元素。

所以給出一些錨點:

<a>Zero</a>
<a>One</a>
<a>Two</a>
<a>Three</a>
<a>Four</a>

你可以做這樣的事情:

$('a').click(function() {
    var i = $('a').index(this);
    // i is the index of the clicked anchor within all the anchors.
});

演示: http//jsfiddle.net/ambiguous/YbUU7/

這個怎么樣:

$('a').click(function(){
console.log($(this).index());
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM