[英]How to find the index of a clicked element from an array with jquery?
How can I find the index of a clicked anchor tag from an array with jquery??? 如何使用jquery ???从数组中找到单击的锚标记的索引?
I want to search if there is an element that eqqal to clicked element, and if is true return the index of that element. 我想搜索是否存在eqqal单击元素的元素,如果为true则返回该元素的索引。
i tried with something like this but it return with -1 我试过这样的东西,但它以-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);
});
please can you help me??! 请你能帮帮我吗??!
I'm not sure what all the get
and makeArray
stuff is for but I think you're looking for index
: 我不确定所有get
和makeArray
内容是什么,但我认为你正在寻找index
:
Search for a given element from among the matched elements. 从匹配的元素中搜索给定元素。
So given some anchors: 所以给出一些锚点:
<a>Zero</a>
<a>One</a>
<a>Two</a>
<a>Three</a>
<a>Four</a>
you could do things like this: 你可以做这样的事情:
$('a').click(function() {
var i = $('a').index(this);
// i is the index of the clicked anchor within all the anchors.
});
Demo: http://jsfiddle.net/ambiguous/YbUU7/ 演示: http : //jsfiddle.net/ambiguous/YbUU7/
How about this: 这个怎么样:
$('a').click(function(){
console.log($(this).index());
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.