如何在 Jquery 中扩展特定父级
我有一个使用 jquery 制作的菜单。我希望当我单击该父项下的特定父项时,应展开其他父项下的所有其他子项,并隐藏。
我有父 ID,因此我可以扩展特定的父 ID。
我的代码如下。
<script>
$(document).ready(function(){
var msg1;
var msg14;
var msg = false;
alert((document.getElementById(''P17_LOCATION_ID'')).value);
alert(msg14);
$(''.dropdown_menu'').click(function(event) {
msg = !msg;
if (msg)'
$(this).parent(''.parent_menu'').find(''.child_menu'').slideDown("slow");
else
$(''.child_menu'').hide();
return false;
});
' });
</script>
这里 - $(this).parent(''.parent_menu'').find(''.child_menu'').slideDown("slow");
负责向下滑动子菜单父母,但目前所有子菜单都在扩展,我想停止,当我单击某个父菜单时,则该父菜单下的子菜单只能显示,以便我如何将 id 传递给上面的语句。 请建议一些解决方案
I have a menu made using jquery.I want that when i click on particular parent only child under that parent shud expand all other child under other parent shud get hide.
I have got parent id so based on that i can expand a particular parent.
My code is as follows.
<script>
$(document).ready(function(){
var msg1;
var msg14;
var msg = false;
alert((document.getElementById(''P17_LOCATION_ID'')).value);
alert(msg14);
$(''.dropdown_menu'').click(function(event) {
msg = !msg;
if (msg)'
$(this).parent(''.parent_menu'').find(''.child_menu'').slideDown("slow");
else
$(''.child_menu'').hide();
return false;
});
' });
</script>
Here - $(this).parent(''.parent_menu'').find(''.child_menu'').slideDown("slow");
is responsible for sliding down of child menu for parents but currently all the child menus are expanding which i want to stop and when i click on some parent then child under that parent only shud be displayed so how i can pass the id to this statement above.
Kindly suggest some solution
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$(expr).parent(sel) 返回 expr 与 sel 匹配的所有父级。你不想 $(this).closest('.parent_menu')
$(expr).parent(sel) returns all parents of expr that match sel. you wan't $(this).closest('.parent_menu')
或者,您也可以仅使用
parent()
返回的集合中的第一个元素:但是,我同意
closest()
更优雅,它基本上执行以下操作:同样的事情。无论如何,希望我的帖子能为您澄清一些事情。Alternatively, you could also use only the first element in the collection returned by
parent()
:However, I agree that
closest()
is more elegant, and it basically does the same thing. Hope my post clarifies things for you, anyway.