我有一个包含3列的表.我需要绑定一个事件,只要使用jQuery单击其中一列,就会触发该事件.
但是,我需要知道单击列的索引.
ie:第一列(索引0),第二列(索引1),第三列(索引2),依此类推......
我怎样才能做到这一点?
var firstRow:
var firstRow = $("tr:first > th", "table[id*=Grid]");
Run Code Online (Sandbox Code Playgroud)
看一看:
firstrow.click(function(e){
//var id = e.target.index;
var id = $(e).parent().children().index(this);//returns -1
})
Run Code Online (Sandbox Code Playgroud)
您可以使用.index()(基于0)来执行此操作,如下所示:
$("td").click(function() {
var i = $(this).parent().children().index(this);
alert(i);
});
Run Code Online (Sandbox Code Playgroud)