admin管理员组文章数量:1278920
I'm working on drop down menu with css and jquery. I wish to be menu open until I click something or until I click out of the menu.
This is what I have tried:
$('#optionButton').click(function() {
$('#dropMenu').css('visibility' , 'visible') //optionButton clicked, menu visible
});
$('*').not('#optionButton').click(function() {
$('#dropMenu').css('visibility' , 'hidden') //clicked eanithing else: menu close
});
But it doesn't work how I expected.
I'm working on drop down menu with css and jquery. I wish to be menu open until I click something or until I click out of the menu.
This is what I have tried:
$('#optionButton').click(function() {
$('#dropMenu').css('visibility' , 'visible') //optionButton clicked, menu visible
});
$('*').not('#optionButton').click(function() {
$('#dropMenu').css('visibility' , 'hidden') //clicked eanithing else: menu close
});
But it doesn't work how I expected.
Share Improve this question edited Mar 14, 2012 at 14:13 Jivings 23.3k7 gold badges62 silver badges102 bronze badges asked Mar 14, 2012 at 14:10 ClemClem 11.8k8 gold badges35 silver badges48 bronze badges4 Answers
Reset to default 4When you click on an DOM object, it does event bubbling, which means it starts from the most specific object and bubbles up the DOM tree until it gets to the document
object. You can prevent an event from bubbling up the DOM treee by returning false
.
$(document).click(function() {
$('#dropMenu').hide();
});
$('#optionButton').click(function() {
$('#dropMenu').show();
return false;
});
Notice how I used the hide
and show
methods instead of css('visibility' , 'visible/hidden')
. These two actually do slightly different things, nut if you just wanted to hide an element, the hide
method is the easiest way to do it in jQuery.
You can see a working example of this on jsFiddle.
Try this:
$("body").click(function(e) {
if ( e.target.id === "optionButton" ) {
$("#dropMenu").css("visibility", "visible");
}
else {
$("#dropMenu").css("visibility", "hidden");
}
});
Or, the shorter version of the same thing:
$("body").click(function(e) {
$("#dropMenu").css("visibility", ( e.target.id === "optionButton" ? "visible" : "hidden" ));
});
I did the experiment with CSS only. May be it's work for your.
Check http://jsfiddle/ZuLHb/1/
You can bind a click event on the document to closez the mùeny:
$(document).click(function() {
$('#dropMenu').css('visibility' , 'hidden') //clicked eanithing else: menu close
});
The important part is that you stop event propagation on the click event of the #optionButton
, otherwise the event will bubble up to the document and execute the click handler attached to it:
$('#optionButton').click(function(e) {
$('#dropMenu').css('visibility' , 'visible') //optionButton clicked, menu visible
e.stopPropagation();
});
DEMO
本文标签: javascriptHide element when focus lostStack Overflow
版权声明:本文标题:javascript - Hide element when focus lost - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741222707a2361271.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论