我正在使用jQuery的.toggle方法来创建菜单,该菜单在单击div时打开和关闭。这里是我的代码:“un”使用jQuery切换div
$(".header").toggle(function() {
$(this).find(".parent").fadeIn("fast");
$(this).css("background-color","red");
}, function() {
$(this).find(".parent").fadeOut("fast");
$(this).css("background-color","white");
});
$(document).click(function() {
$('.parent').fadeOut("fast");
$(".header").css("background-color","white");
});
$(".parent").click(function(event){
event.stopPropagation();
});
http://jsfiddle.net/bmcmahen/X9S5C/8/
这工作顺利,直到我点击弹出菜单以外,关闭它,然后尝试再次单击菜单按钮。然后它需要双击。我需要做的是从$(document).click函数中解开对div的点击。有关我如何做这件事的任何想法?
我一般从来不用切换,而不是我用一个“点击”听众,并依靠'问题'当前状态使用'.is(':hidden')' – anson 2012-04-02 23:54:02
我正在考虑做类似的事情 - 我会给它一个镜头。 – bento 2012-04-03 00:02:55