识别 jquery-ui 对象时出现问题
我在使用 jQuery 识别项目时遇到一个小问题。让我更好地解释一下:)
当用户单击按钮时,我会出现一个菜单。这工作得很好。然后,我希望当用户单击网站上除菜单之外的任何部分时,此菜单消失。这也很好用,但有一个小问题:在菜单中,有一个项目允许用户从自动完成列表中进行选择。该列表是使用 jquery-ui 自动完成完成的。当用户单击此处的某个项目时,菜单消失(它不应该:)),并且我不知道如何对此进行例外处理,因为我不知道如何获取该项目,我可以吗按班级抢吗?有id吗?
我有以下代码来识别单击的位置并继续(您可以看到,如果单击菜单按钮或菜单,菜单将不会隐藏)。在 jQuery-ui 自动完成中单击时如何例外?
$(document).click(function(event) {
if($(event.target).parents().index($('#menu')) == -1 && $(event.target).parents().index($('#menu-button')) == -1) {
if($('#menu').is(":visible")) {
$('#menu').hide(500);
}
}
});
谢谢!
I have a small problem identifying an item with jQuery. Let me explain better :)
I have a menu that appears when the user clicks on a button. This is working perfectly. Then, I want this menu to go away when the user clicks on any part of the site except on the menu. This is also working great, but with a small problem: in the menu, there is an item that allows the user to choose from an autocomplete list. This list was done using jquery-ui autocomplete. When the user clicks on an item here, the menu disappears (it shouldn't :) ), and I don't know how to make the exception for this, since I don't know how can I grab the item, can I grab it by class? Does it have an id?
I have the following code to identify where the click is being done and proceed (you can see that if menu-button or menu are clicked the menu will not hide). How do I make the exception when the click is made in autocomplete of jQuery-ui?
$(document).click(function(event) {
if($(event.target).parents().index($('#menu')) == -1 && $(event.target).parents().index($('#menu-button')) == -1) {
if($('#menu').is(":visible")) {
$('#menu').hide(500);
}
}
});
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
自动完成
ul
有一个ui-autocomplete
类。因此,您可以通过执行$(".ui-autocomplete")
来获取它。但是,我的首选方法是执行以下操作:
这基本上会阻止点击冒泡,因此不会触发文档点击。
The autocomplete
ul
has a class ofui-autocomplete
. So you can get it by doing$(".ui-autocomplete")
.However, my preferred method for doing what you are trying to do would be to do this:
This basically will stop the click from bubbling up, so it will not trigger the document click.
您可以按类别拾取这些物品。
我想我会把它改成:
You can pick up those items by their class.
I think I would change it to be: