单击 jquery 中的子菜单时如何保留父级详细信息
我在 oracle apex 中使用 jQuery 设计了菜单。菜单是这样的
Parent1
child1
child2
parent2
child3
child4
parent3
child5
child6
我的问题是,当我单击parent1时,只有child1和child2应该显示,但在我的情况下,每个父级都会展开。用户可以看到所有的孩子。这不应该发生,用户应该只能看到单击的父级和子级详细信息。
我的代码如下。
$(document).ready(function() {
var msg = false;
$('.dropdown_menu').click(function() {
msg = !msg;
if (msg)
$(this).parents('.parent_menu').find('.child_menu').slideDown("slow");
else
$('.child_menu').hide();
return false;
});
});
I have menu designed using jQuery in oracle apex.The menu is like this
Parent1
child1
child2
parent2
child3
child4
parent3
child5
child6
My problem is when I click on parent1 only child1 and child2 should display but in my case each parent gets expanded. And the user can see all the childs. Which should not happen users should only be able to see the clicked parent and child details.
My code is as follows.
$(document).ready(function() {
var msg = false;
$('.dropdown_menu').click(function() {
msg = !msg;
if (msg)
$(this).parents('.parent_menu').find('.child_menu').slideDown("slow");
else
$('.child_menu').hide();
return false;
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您提供一些 HTML,将会有很大帮助。所以,我假设您正在使用此 HTML抱歉,我看到您确实提供了一些内容的评论。该脚本应与您提供的 HTML 配合使用:
编辑:我进行了以下更改:
dropdown_menu
链接,因此child_menu
链接现在可以使用#17
)到链接的末尾:http://www.mysite.com/somepage.htm#17
。该脚本将获取此 URL 并查找位置 ID 为17
,并在href
末尾找到 ID 时打开比利时子菜单(请参阅此 我使用的选择器上的 jQuery 参考)。使用此 HTML(您在下面的评论中提供):
使用此脚本:
最后,您刚刚在评论中添加的脚本,除非您也提供 HTML,否则我无法对其执行任何操作。
It would have helped a great deal if you would have provided some HTML. So, I'm assuming you are using this HTMLSorry I see the comment where you did provide some.This script should work with the HTML you provided:
Edit: I made the following changes:
dropdown_menu
links, so thechild_menu
links will now work#17
) to the end of the link:http://www.mysite.com/somepage.htm#17
. The script will take this URL and find the location ID is17
and open the Belgium child menu when it finds the ID at the end of thehref
(see this jQuery reference on the selector I used).Using this HTML (you provided in a comment below):
With this script:
Lastly, the script you just added in the comments, I can't do anything with that unless you provide the HTML as well.
尝试:
Try:
只需将父母更改为单亲父母即可。虽然两者都找到元素的祖先,但范围不同。
.parent() 仅搜索直接父级。
.parents() 查找 DOM 树根部之前的所有父级。
编辑1:我一定是愚蠢的,虽然上面是正确的,但您指示单击元素的父级,而不是子级 - 现在我戴上了眼镜:
.children() 将是该元素的正确选择器(来自父级)当然,正如您所指出的,
因此 $(this).children(). 代码可以在这里执行您想要的操作
:
这将从我的父级中选择 .parent_menu 项目,其中显示“所有 .parent_menu 项目和其中的所有 .child_menu
仅选择
我的(此)直接子级,如果您碰巧有其他子级(其他类),您可以进一步指定它:
Edit2:旁注:不清楚您是否有该类。 .click 事件应用于每个父级(parent1、parent2 等)或应用于parent1、parent2 等的父级,这将更改 .click 事件捕获的范围,我假设该类应用于parent1, Parent2 等级别。
Just change parents to the singular parent. While both find the ancesters of the element, the scope is different.
.parent() searches the immediate parent only.
.parents() finds all parents up to the base of the DOM tree.
EDIT1: I must be dumb, while the above is true, you indicate to click the PARENT of the elements, not the children - now that I have my glasses on:
.children() would be the correct selector of that (from the parent of the children of course, as you indicated.
so $(this).children(). code to do what you want here.
change:
This will select the .parent_menu items children from my parent, which says "all .parent_menu items and all .child_menu within that.
to
This select only MY (this) immediate children. IF you happen to have other children (other classes) you can specify it futher with:
Edit2:one side note: it is unclear if the class you have the .click event on is applied at each parent (parent1, parent2 etc) or to the parent of parent1, parent2 etc. which would change the scope of the .click event capture, I make the assumption that the class is applied at the parent1, parent2 etc level.