如何通过单击页面中的其他位置来取消切换下拉菜单 - stopPropagation?
我想知道是否有人可以帮助最终解决我不久前提出的问题。
我无法通过单击按钮外部或页面上的其他任何位置来取消切换这些下拉菜单。
请参阅此jsFiddle。
我已经看到有人使用 stopPropagaton()
但我不确定如何在这里应用它。
有什么想法如何做到这一点?
我的切换代码:
var cur = null;
$(".toggle").click(function(e){
$('#nav ul:visible').hide();
if(cur == null || cur.currentTarget != e.currentTarget)
{
if(cur != null)
{
$(cur.currentTarget)
.children('a:first').children('span').removeClass('fc-state-active');
}
cur = e;
$(cur.currentTarget)
.children('a:first').children('span').addClass('fc-state-active');
$(cur.currentTarget)
.children('ul').show();
}
else
{
$(cur.currentTarget)
.children('a:first').children('span').removeClass('fc-state-active');
cur = null;
}
});
I wonder if anyone can help to finally resolve an issue I brought up on SO a while back.
I am unable to untoggle these dropdown menus by clicking outside of the button, or anywhere else on the page.
Please see this jsFiddle.
I've seen folks using stopPropagaton()
but am unsure how to apply it here.
Any ideas how to do this?
My toggling code:
var cur = null;
$(".toggle").click(function(e){
$('#nav ul:visible').hide();
if(cur == null || cur.currentTarget != e.currentTarget)
{
if(cur != null)
{
$(cur.currentTarget)
.children('a:first').children('span').removeClass('fc-state-active');
}
cur = e;
$(cur.currentTarget)
.children('a:first').children('span').addClass('fc-state-active');
$(cur.currentTarget)
.children('ul').show();
}
else
{
$(cur.currentTarget)
.children('a:first').children('span').removeClass('fc-state-active');
cur = null;
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信以下内容应该适合您。这利用了 jQuery 的 focusout() 函数:
这是一个更新的小提琴: jSFiddle
编辑:以下内容就职于 FF & Chrome 浏览器
新小提琴: jsFiddle
原因:
$(document).click()
是调用之后$(".toggle").click()
编辑2:工作小提琴可以在这里找到:jSFiddle
I believe the following should work for you. This utilizes jQuery's focusout() function:
And here's an updated fiddle: jSFiddle
EDIT: The following works in FF & Chrome
New Fiddle: jsFiddle
Reason:
$(document).click()
is called after$(".toggle").click()
EDIT 2: The working fiddle can be found here: jSFiddle