如何处理< tr>的点击但不是儿童元素? [英] How to handle a click on a <tr> but not on the child elements?

查看:67
本文介绍了如何处理< tr>的点击但不是儿童元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码处理点击。

I handling the click with following code.

带输入的表格

<table>
    <tr>
        <td>
            <input type="checkbox" />
        </td>
    </tr>
</table>​

点击处理程序

$('table tr').click(function(){
    alert('clicked');
});​

http ://jsfiddle.net/n96eW/

它运行良好,但是如果我在td中有一个复选框,它在点击时也会处理它。

It's working well, but if I have a checkbox in the td, it's handling it too when clicked.

有没有办法处理TR的点击但不触发子元素?

Is there a way to handle the click of the TR but not trigger on the child elements?

推荐答案

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();
});
​

这篇关于如何处理&lt; tr&gt;的点击但不是儿童元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆