元素索引,jQuery或Javascript [英] Index of elements, jQuery or Javascript
本文介绍了元素索引,jQuery或Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含3列的表。我需要绑定一个事件,只要使用jQuery单击其中一个列就会触发该事件。
I've a table that contains 3 columns. I need to bind an event that fires off whenever one of those columns is clicked using jQuery.
但是,我需要知道单击列的索引。
However, I need to know the index of the column clicked.
ie:第一列(索引0),第二列(索引1),第三列(索引2)等等......
i.e: First column (index 0), Second column (index 1), Third column (index 2), and so on...
我该怎么做?
var firstRow:
var firstRow:
var firstRow = $("tr:first > th", "table[id*=Grid]");
看看:
firstrow.click(function(e){
//var id = e.target.index;
var id = $(e).parent().children().index(this);//returns -1
})
推荐答案
您可以使用 .index()$ c $来完成此操作c>
(基于0),如下所示:
You can do this using .index()
(it's 0 based), like this:
$("td").click(function() {
var i = $(this).parent().children().index(this);
alert(i);
});
这篇关于元素索引,jQuery或Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文