我有一个JQuery的小问题。jquery和点击隐藏div
嗯,我有一个,我想隐藏这个div,当用户点击不在Facebook中的“通知”行为的区域。
我发现的解决方案是使用jQuery.live()方法,但我认为有更好的方法来做到这一点。
谢谢。
我有一个JQuery的小问题。jquery和点击隐藏div
嗯,我有一个,我想隐藏这个div,当用户点击不在Facebook中的“通知”行为的区域。
我发现的解决方案是使用jQuery.live()方法,但我认为有更好的方法来做到这一点。
谢谢。
假设:
<div class="notification">You have 3 new messages</div>
使用:
$(document).click(function(evt) {
if ($(this).closest("div.notification").length == 0) {
$("div.notification").fadeOut();
}
});
基本上这监听所有点击。如果收到的通知内没有发生这种情况,它会将其淡出。
谢谢您的回答,但在:
$(this).closest("div.notification").length == 0)
总是返回我0(即使我在div点击),所以在div总是隐藏。
这是我的代码:
$(document).click(function(evt) {
if ($(this).closest("div#show_notif").length==0)
$("div#show_notif").fadeOut();
});
和HTML:
<div id="click_show_notif" onclick="$('div#show_notif').show();"><img src="http://'+STATIC_SERVER+'/images/notif.png" /><div id="show_notif"></div>
有件事我忘了吗?
试试这个:
$("#click_show_notif").live('click',function(e) {
$("#show_notif").show();
return false;
});
$('body').live('click',function(e) {
if ($(this).closest("div#show_notif").length==0) {
$("div#show_notif").hide();
}
});
如果点击是在做,会发生什么'div.notification'本身? – rahul 2010-04-26 04:13:13
@rahul不得不对这种情况做一个小小的修改('parents()'到'closest()'),但基本上它不会在点击时淡出它们。 – cletus 2010-04-26 04:18:07
为你+1并删除了我的答案。 – rahul 2010-04-26 04:21:06