我正在尝试使用以下方法从点击事件处理程序中排除图像:
$('#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/