jquery - 从事件处理程序中排除元素

标签 jquery css

我正在尝试使用以下方法从点击事件处理程序中排除图像:

$('#templtable .trhover *:not(#infoimg)').live('click', function(event) {
      event.stopPropagation();
      $(this).toggleClass('selected');

$('#infoimg').click(function(event) {
          console.log("infoimg");
    event.stopPropagation();
});

表格看起来像这样:

<table><tr class='trhover'><td><img id='infoimg' src='/images/icon-info.gif' alt='Details'><input type='checkbox' class='followup' id='1' value='234234'></td><td>234234</td></tr><table>

但是图片上的点击事件仍然有效,请问如何解决这个问题?

谢谢

最佳答案

您的代码无效。无论如何,如果您想从执行点击事件中排除 img,请使用选择器 $('tr *:not(img)')。但它会在您单击 img 时触发事件,因为当事件冒泡时。所以你必须绑定(bind)一个事件来停止传播。

$('.trhover *:not(#infoimg)').bind('click', function(event) {
    event.stopPropagation();
    $('body').append('click -> '+event.currentTarget.tagName+'<br />');
});
$('#infoimg').click(function(event) {
    event.stopPropagation();
});

试试这个:http://jsfiddle.net/pNaaD/
添加行的版本:http://jsfiddle.net/Zxk8R/

关于jquery - 从事件处理程序中排除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9224343/

相关文章:

javascript - 开发一个简单的网页设计器(拖放文本/w 字体和图像)

javascript - 网站右侧的 chrome 中每隔几秒就会有一个框闪烁

html - 垂直对齐包含在 <td> 中的 <div>

html - 添加页脚,当内容不多时,该页脚会粘在页面底部

jquery - 使用 jQuery css 显示 -webkit-transition

javascript - 如何在每次点击时打开新窗口

javascript - 捕获停止传播的事件

javascript - Selectize.js:使用默认值填充

html - CSS div 高度 100% 布局

html - 带溢出的 IFRAME :hidden still shows scrollbars in Chrome