[英]Hide div when clicking document but not when clicking a particular link
$(document).click( function () {
$('#my_div').fadeOut(350);
});
I want to hide this div when user clicks into the document but not when he clicks in e particular link for example #my_link. 我想在用户单击文档时隐藏此div,但在他单击e特定链接(例如#my_link)时不希望隐藏。 How can i do this? 我怎样才能做到这一点? Thanks 谢谢
A simple way is to add an event to your link and use stopPropagation
: 一种简单的方法是将事件添加到链接并使用stopPropagation
:
$('#my_link').click(function(e){
e.stopPropagation();
})
Pass the event object to your click function and check for the id: 将事件对象传递给您的click函数,并检查ID:
$(document).click( function (event) {
var idName = event.target.id;
if(idName == "my_link"){
return false;
};
$('#my_div').fadeOut(350);
});
This way you don't have to add an extra Event Listener :) 这样,您不必添加额外的事件监听器:)
This method is also extensible, must you wish to include other links in the future... 此方法也是可扩展的,如果您将来希望包含其他链接...
使用stopPropagation( http://api.jquery.com/event.stopPropagation/ )
$('a#my_link').click(function(event) { event.stopPropagation(); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.