[英]how to get the td clicked having tr
I have a tr
with a click listener. 我有一个带有点击监听器的
tr
。 I want to know which td
was the one clicked: 我想知道单击哪个
td
:
<tr class ="clickingClass">
<td> first td </td>
<td> second td </td>
</tr>
$(".clickingClass").on('click', function() {
$(this).closest('td');
});
The above shows nothing. 上面没有显示任何内容。 How can I do this?
我怎样才能做到这一点?
The issue is because closest()
goes up the DOM, not down it. 问题是因为
closest()
在DOM上而不是在DOM上。
To do what you require you could instead attach the event handler directly on the td
elements: 要执行您需要的操作,您可以将事件处理程序直接附加到
td
元素上:
$(".clickingClass td").on('click', function() {
// do something with $(this)...
});
Alternatively you could use the target
property of the event passed to the handler function to determine which td
was clicked: 或者,您可以使用传递给处理程序函数的事件的
target
属性来确定单击了哪个td
:
$(".clickingClass").on('click', function(e) {
// do something with $(e.target)...
});
You can use event.target
for this 您可以为此使用
event.target
$(".clickingClass").on('click',function(event){
alert(event.target);
});
Why not bind to the td
s instead? 为什么不绑定到
td
呢?
$(".clickingClass td").on('click',function(){
this //td
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.