jquery - 单击 <tr>,但前提是单击的 <td> 不是 <a>

标签 jquery html-table

我有一个结构如下的表:

 <table>
 <tr id="ui-id-7" onclick="editThis();">
 <td>100</td>
 <td>Test</td>
 <td class="noEdit"><a href="www.mysite.com" target="_blank"><img src="www.mysite.com/test.jpg"></a></td>
 </tr>
 </table>

我需要的是:整个<tr>可点击并调用 editThis()方法,除非用户单击 <td>具有 link 元素(如果是 noEdit 类的元素)。

在上面的代码中,单击 ANY <td> (或 <tr> )将调用 editThis()方法。我知道我可以为每个 <td> 编写调用而不是 <tr> ,但这会写很多东西......

这可能吗?

最佳答案

使用event.stopPropagation()

$('#ui-id-7 td.noEdit').click(function(e){
   e.stopPropagation();
});

Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event

关于jquery - 单击 <tr>,但前提是单击的 <td> 不是 <a>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21915341/

相关文章:

javascript - 模拟 div 内的 href 的点击

html - 在html中制作自定义表格

jquery - 如何使表格行逐行动画

javascript - 表行表现得像一个链接

javascript - 自定义 CSS 不适用于带有 jQ​​uery 的 Body 元素

javascript - 使用 Jquery 垂直选择

javascript - 处理第三方库发送的ajax请求的开始和结束事件

jquery - 从一个父级链接 jquery 方法的更好方法

css - 神秘的底边

php - 无法使用 INSERT INTO 向 mysql 表中插入数据