我试图使用下面的代码获取 html 表中所选行的行索引,但它不起作用。
$(function() {
$("#myTable tr").click(function() {
alert(this.rowIndex);
});
});
上面的代码肯定有什么问题?
最佳答案
你的代码看起来不错,除非你没有像其他所说的那样添加 jQuery 引用 AND/OR 也许你的表是动态创建的,请尝试使用 .on
作为 事件委托(delegate)
就像这样:
$(function() {
// use event delegation
$(document).on('click','#myTable tr', function() {
alert(this.rowIndex);
// or
alert($(this).index()); // jQuery way
});
});
关于jquery - 如何使用jquery获取html表中所选行的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32982938/