如何在 Jquery 中扩展特定父级

发布于 2024-08-13 08:26:26 字数 700 浏览 2 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

并安 2024-08-20 08:26:26

$(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')

肥爪爪 2024-08-20 08:26:26

或者,您也可以仅使用 parent() 返回的集合中的第一个元素:

$(this).parent('.parent_menu').eq(0).find('.child_menu').slideDown('slow');

但是,我同意 closest() 更优雅,它基本上执行以下操作:同样的事情。无论如何,希望我的帖子能为您澄清一些事情。

Alternatively, you could also use only the first element in the collection returned by parent():

$(this).parent('.parent_menu').eq(0).find('.child_menu').slideDown('slow');

However, I agree that closest() is more elegant, and it basically does the same thing. Hope my post clarifies things for you, anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文