[英]How to handle a click on a <tr> but not on the child elements?
I handling the click with following code.我使用以下代码处理点击。
Table with input带输入的表
<table>
<tr>
<td>
<input type="checkbox" />
</td>
</tr>
</table>
Click handler单击处理程序
$('table tr').click(function(){
alert('clicked');
});
http://jsfiddle.net/n96eW/ http://jsfiddle.net/n96eW/
It's working well, but if I have a checkbox in the td, it's handling it too when clicked.它运行良好,但如果我在 td 中有一个复选框,它也会在单击时处理它。
Is there a way to handle the click of the TR but not trigger on the child elements?有没有办法处理 TR 的点击但不触发子元素?
http://jsfiddle.net/n96eW/1/ http://jsfiddle.net/n96eW/1/
Add another event handler in your checkbox to stopPropagation:在您的复选框中添加另一个事件处理程序以停止传播:
$('table tr').click(function(){
alert('clicked');
});
$('table tr input').click(function(e) {
e.stopPropagation();
});
You can check event.target
to filter your events:您可以检查
event.target
来过滤您的事件:
$('table tr').click(function(e){
if(e.target.tagName.toLowerCase() != "input") {
alert('clicked');
}
});
You could also use你也可以使用
$("tr").on('click',function() {
if (!$(event.target).is('input'))
alert('clicked');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.