Best way to "click away" from a drop down menu?
Possible Duplicate:
How to detect a click outside an element?
I have a drop down menu that appears on click. When the user clicks away from it, it disappears.
For the on click Im using:
$("#title").click(function() {
dropdown_show();
);
But when the user clicks away, Im using:
$('body').click(function(e) {
if ((!$(e.target).is('#title'))&&(!$(e.target).is('#dropdown'))) {
dropdown_hide();
}
});
Is there a better way to know when a user clicks away without having to run an event every single time the user clicks on the body?
You could perhaps use the focus event, So if its not in focus you hide it. Here is jquery's api for it
You could only set the body click event inside the menu click event, and use .unbind to remove the body click event when it gets clicked.
You might want to use .bind with a namespace: Best way to remove an event handler in jQuery?
链接地址: http://www.djcxy.com/p/14712.html上一篇: 切换点击效果
下一篇: 从下拉菜单中“点击”的最佳方式?