我正在使用“live”功能在表格行上执行一些点击操作,即
$("tr").live('click',function() {
alert('Some table row is clicked');
});
我想找出哪一行被点击,并使用if-else
,基于此给出一些自定义警报。谁能告诉我该怎么做?
非常感谢。
编辑1:
有没有办法可以引用函数内单击的行的元素?
最佳答案
$("tr").live('click', function() {
if (this.id == "foo") {
alert('the tr with the foo id was clicked');
}
});
如果您想检查哪个行号,请使用index
:
$("tr").live('click', function() {
if $(this).index() === 2) {
alert('The third row was clicked'); // Yes the third as it's zero base index
}
});
<小时/>
更新:
$("tr").live('click', function() {
// "this" is the clicked <tr> element
// $(this).find('td span') is the spans inside a td inside the clicked <tr>
}
关于jquery - 如何使用 jQuery 找出哪个表行被单击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10469334/