[英]Onblur event mistakenly fires in IE when parts of popup document are clicked
[英]How to bind a click event to the document so that it fires only when no object is clicked
我希望每当单击DOM元素以外的内容时触发一个事件,而当单击图像时触发一个单独的事件。
现在我有:
$( document ).click( function() { /*do whatev*/ } );
在另一个地方:
$( "img" ).click( function( e ) {
e.stopPropagation();
/*do whatev*/
} );
这是行不通的。 这两个事件都被触发。 还有其他想法吗?
简单明了:
jQuery(document).click(function(event) {
if (jQuery(event.target).is('img'))
{
alert('img');
}
else
{
// Reject event
return false;
}
});
如果用户单击img元素,则会提示“ img”,否则将停止单击事件。
像这样的东西:
$('*').click(function(ev) {
ev.stopPropagation();
if ($(this).is('img')) alert('img');
else if($(this).is('div')) alert('div');
else alert('something else');
});
这应该可以,但是仅img
是不够的。 它可能是从img容器触发的。 我认为您需要一个选择器,例如$('p,div,img,a,input,textarea,span,ul,ol,dl,li,dd,dt,table,tr,td')
和任何其他标签可以想到将其完成。 这可能存在性能问题。
如果您希望在单击html
和/或body
或单击img
时触发事件,则应执行以下操作: 实时示例
$(document).click(function(e) {
if($(e.target).is('html')){
alert('this is the html element');
}else if($(e.target).is('body')){
alert('this is the body element');
}
});
$("img").click(function(e) {
e.stopPropagation();
alert('img')
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.