表格行的点击事件-仅在“没有点击"的情况下触发 [英] Click event on table row - only trigger if nothing else is 'clicked'

查看:187
本文介绍了表格行的点击事件-仅在“没有点击"的情况下触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用一些jQuery代码时遇到了一些问题,我希望并认为该解决方案相对简单.

I'm having some issues with some jQuery code, and I'm hoping and thinking that the solution is relatively simple.

说我有一个像这样的表:

Say I have a table like:

<table>
   <tr>
     <td><input type="checkbox" name="hai" value="der" /></td>
     <td><a href="mypage.php">My link</a></td>
     <td>Some data</td>
     <td>Some more data</td>
   </tr>
   ...
</table>

现在通过JavaScript,我在jQuery(document).ready();中执行以下操作

Now through JavaScript I do the following in jQuery(document).ready();

jQuery("table tr").click(function(){
   var row = jQuery(this);
   var hasClass = row.hasClass("highlight");
   jQuery("table tr").removeClass("highlight");
   if(!hasClass){
     row.addClass("highlight");
   }
});

所有这些都是非常简单的东西-我希望能够单击一行并使其突出显示,并且一次只能突出显示一行(显然,在我的实际代码中,我还可以做得更多)我的问题:

All of this is pretty simple stuff - I want to be able to click a row and have it being highlighted and only one row can be highlighted at a time (obviously in my actual code I do more than that) - now here's my problem:

当用户单击锚标记或复选框时,这也会触发行单击事件,而我无法确定如何将其过滤掉?我知道我需要在我的点击处理程序中包含该事件,但是如何以一种在尽可能多的浏览器中都可以使用的方式检查该事件呢?

When a user clicks the anchor tag, or the checkboxes, this triggers the row click event as well and I cannot work out how to filter that out? I know I need to include the event in my click handler, but how to check for this in a such way that it works in as many browsers as possible?

推荐答案

尝试将处理程序添加到其他响应单击事件的元素,并阻止事件冒泡.这将是您已经拥有的东西的补充.

Try adding handlers to the other elements that respond to click events and stop the event from bubbling up. This would be in addition to what you already have.

jQuery('table input, table a').click( function(e) {
    e.stopPropagation();
    return true;
}):

这篇关于表格行的点击事件-仅在“没有点击"的情况下触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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