HTML + jQuery 下拉菜单:当单击页面的任何其他部分时,FadeOut?
这可能是一个简单的问题,但本质上我正在设计一个 HTML 下拉列表。
$('#bible-trans').click(function() {
$('#bible-translation-list').fadeToggle('fast');
});
其中#bible-trans
是主下拉按钮,下拉列表的内容是#bible-translation-list
。因此,当我点击主下拉菜单时,内容会切换。简单的。
我想做的是,如果用户点击页面上的其他任何位置,下拉菜单就会淡出。
$("*").not('#bible-trans').click(function() {
$('#bible-translation-list').fadeOut();
});
这就是我现在所拥有的,但我很确定它是不正确的 - 显然是因为它不起作用 - 当我单击切换#bible-trans
时,它会切换然后消失立即离开。我是否正确使用了 not()
选择器?
编辑:我认为这与 #bible-trans
是 * 的子级(显然)这一事实有很大关系。我有什么办法可以解决这个问题吗?
This is probably a simple question, but essentially I'm designing an HTML dropdown.
$('#bible-trans').click(function() {
$('#bible-translation-list').fadeToggle('fast');
});
Where #bible-trans
is the main dropdown button, the content of the dropdown is #bible-translation-list
. So when I hit the main dropdown, the content toggles. Simple.
What I'd like to do is if the user hits anywhere ELSE on the page the dropdown fades out.
$("*").not('#bible-trans').click(function() {
$('#bible-translation-list').fadeOut();
});
This is what I have right now, but I'm pretty sure it's incorrect—well it obviously is because it doesn't work— when I click to toggle #bible-trans
, it toggles and then fades away immediately. Am I using the not()
selector correctly?
EDIT: I think this has a lot to do with the fact that #bible-trans
is a child of * (obviously). Any way that i can work through that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种可能的方法
http://jsfiddle.net/76gUj/29/
另一个例子mu 太短
http://jsfiddle.net/ambigously/76gUj/32/
Heres one way possible of doing it
http://jsfiddle.net/76gUj/29/
Another example by mu is too short
http://jsfiddle.net/ambiguous/76gUj/32/
如果您确定用户将单击 bible-trans 下拉列表,您可以将“blur”事件绑定到它。
这取决于下拉列表在某个时刻的焦点。
If you're sure that the user will be clicking on the bible-trans dropdown, you could bind the "blur" event to it.
This depends on the dropdown being focused at some point.