Jquery 切换菜单/显示和隐藏功能

发布于 2024-12-22 11:33:25 字数 2972 浏览 4 评论 0原文

我正在使用 Jquery Toggle 侧边栏菜单,因此菜单中有一个向下箭头,可以在每次单击菜单时向下推动菜单以显示子菜单。我的问题是,当没有子菜单/子菜单时,向下箭头仍然显示,当您单击它时,它会吃掉下面的同级菜单。我正在使用 Silverstripe 中的菜单。如何使箭头仅在有子菜单/子菜单时显示?

/------------------------------------------------------------ -这是我的切换菜单js-------------------------------------------------------- -------/

$(document).ready(function() {

$('.second_level').hide();

$("div.menu > h3").css("background", "url(themes/tutorial/images/menuarrowdown.gif) no-repeat right");

$('div.menu > h3').click(function() {

$(this).next().slideToggle('fast', function() {

//set arrow depending on whether menu is shown or hidden
if ($(this).is(':hidden')) {

$(this).prev().css("background", "url(themes/tutorial/images/menuarrowdown.gif) no-repeat right");
} else {
$(this).prev().css("background", "url(themes/tutorial/images/menuarrowup.gif) no-repeat right");
}
return false;
});
});
});

/---------------------------------- --这是我的html代码-------------------------------------------------------- ---/

<div id="productmenu"> <!-- productmenu starts -->
<div class="menu">                      
  <h3><a href="#">Category 1</a></h3>
    <ul class="second_level">
      <li><a href="page.html">Option 1</a></li>
      <li><a href="page.html">Option 2</a></li>
   </ul>
</div> <!-- /menu -->

<div class="menu">

   <h3><a href="#">Category 2</a></h3>

</div> <!-- /menu -->

<div class="menu">
  <h3><a href="#">Category 3</a></h3>
     <ul class="second_level">
      <li><a href="page.html">Option 1</a></li>
       <li><a href="page.html">Option 2</a></li>
      <li><a href="page.html">Option 3</a></li>
    </ul>
</div> <!-- /menu -->
</div><!-- /productmenu -->

/------------------------------------这里是我的页面.ss-------------------------------------------------------- /

  <% control Menu(2) %>

     <h3><a href="$Link" title="Go to the &quot;{$Title}&quot; page">$MenuTitle</a></h3>

        <% if Children %>   <ul class="second_level">
              <% control Children %>
               <li class="$LinkingMode"><a href="$Link" title="Go to the &quot;{$Title}&quot; page">$MenuTitle</a></li>
              <% end_control %></ul>

        <% end_if %>
  <% end_control %>

感谢您的帮助。请参阅下面的示例图片。

谢谢 Sam

在此处输入图像描述

编辑:要解决吃掉菜单的问题,只需移动 <% control Menu( 2)%>上面和 <% end_control %>在菜单的 finish div 下方。 Js解决方案是使用Matt的新Js。谢谢马特、伊恩克和米洛!感谢您的帮助!

I am using a Jquery Toggle sidebar menu, so there is a down arrow in the menu to push the menu down to show sub-menu(s) every time you click on it. My problem is when there is no child menu/sub-menu, the down arrow is still showing up, and when you click on it, it eats the same level menu below. I am using the menu in Silverstripe. How can I make the arrow shows only when there is a child menu/submenu?

/-----------------------------------------------Here is my toggle menu js--------------------------------------------------/

$(document).ready(function() {

$('.second_level').hide();

$("div.menu > h3").css("background", "url(themes/tutorial/images/menuarrowdown.gif) no-repeat right");

$('div.menu > h3').click(function() {

$(this).next().slideToggle('fast', function() {

//set arrow depending on whether menu is shown or hidden
if ($(this).is(':hidden')) {

$(this).prev().css("background", "url(themes/tutorial/images/menuarrowdown.gif) no-repeat right");
} else {
$(this).prev().css("background", "url(themes/tutorial/images/menuarrowup.gif) no-repeat right");
}
return false;
});
});
});

/------------------------------------Here is my html code----------------------------------------------/

<div id="productmenu"> <!-- productmenu starts -->
<div class="menu">                      
  <h3><a href="#">Category 1</a></h3>
    <ul class="second_level">
      <li><a href="page.html">Option 1</a></li>
      <li><a href="page.html">Option 2</a></li>
   </ul>
</div> <!-- /menu -->

<div class="menu">

   <h3><a href="#">Category 2</a></h3>

</div> <!-- /menu -->

<div class="menu">
  <h3><a href="#">Category 3</a></h3>
     <ul class="second_level">
      <li><a href="page.html">Option 1</a></li>
       <li><a href="page.html">Option 2</a></li>
      <li><a href="page.html">Option 3</a></li>
    </ul>
</div> <!-- /menu -->
</div><!-- /productmenu -->

/------------------------------------Here is my page.ss----------------------------------------------/

  <% control Menu(2) %>

     <h3><a href="$Link" title="Go to the "{$Title}" page">$MenuTitle</a></h3>

        <% if Children %>   <ul class="second_level">
              <% control Children %>
               <li class="$LinkingMode"><a href="$Link" title="Go to the "{$Title}" page">$MenuTitle</a></li>
              <% end_control %></ul>

        <% end_if %>
  <% end_control %>

Your help is appreciated. Please see example pic below.

Thanks
Sam

enter image description here

Edit: To fix the eating up menu issue, just move <% control Menu(2) %> above and <% end_control %> below the finish div of menu. Js solution is to use Matt's new Js. Thanks Matt, Iank and Milo! I appreciate your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

静谧幽蓝 2024-12-29 11:33:25

如果不存在子项,您永远不会告诉系统不打印向下箭头。它将箭头添加到菜单中存在的每个 H3 中,并且从不考虑它是否有子项。因此我们需要检查每个菜单项以查看它是否有子项。以下应该做到这一点:

$("div.menu").each(function(){

  if($(this).children().length > 1) // See if the H3 is the only child
  {
      $(this).children("H3").css("background", "url(themes/tutorial/images/menuarrowdown.gif) no-repeat right");
  }

});

目前我正在午休,没有时间对此进行全面测试。我没有验证这是否有效。

编辑:Sam 放入纯 HTML 后修改代码。

EDIT2:Sam 提供了小提琴后,我将其分叉并进行了更正。小提琴: http://jsfiddle.net/GvGoldmedal /Wp2em/

You never tell your system to not print the down arrow if no child exists. It adds the arrow to every H3 that exists in them menu and never considers if it has children or not. So we need to check each menu item to see if it has children. The following should do that:

$("div.menu").each(function(){

  if($(this).children().length > 1) // See if the H3 is the only child
  {
      $(this).children("H3").css("background", "url(themes/tutorial/images/menuarrowdown.gif) no-repeat right");
  }

});

Presently I'm on lunch break and do not have time to fully test this. I have no verified this will work.

EDIT: Modified code after Sam put in his pure HTML.

EDIT2: After Sam supplied a fiddle, I forked it and made corrections.Fiddle: http://jsfiddle.net/GvGoldmedal/Wp2em/

看透却不说透 2024-12-29 11:33:25

我不明白您用来创建菜单的模板语言,但我也许可以在 jQuery 方面提供帮助。如果您能提供最终生成的菜单 html,将会有很大帮助。

我假设您的 div 元素是菜单项,您的 h3 元素是菜单项内容,您的 ul 是子菜单。如果这是正确的,您可以通过将以下行添加到主函数来隐藏箭头:

$('div.menu:has(ul.second_level) > h3').css('background', '');

如果您发现此代码太慢,您可以识别服务器端没有子菜单的菜单,并给它们一个类,例如 menu_no_submenu ,因此您可以用更快的 '.menu_no_submenu' 替换上面代码中的复杂选择器,或者更好的是,在 css 中隐藏整个箭头。

I don't understand the templating language you are using to create your menu, but I might be able to help with jQuery. It would be a lot of help if you could provide the final generated html of your menu.

I assume that your div elements are a menu item, your h3 elements a menu item content, and your ul is a submenu. If this is correct, you could hide the arrows by adding the following line to your main function:

$('div.menu:has(ul.second_level) > h3').css('background', '');

If you find this code to be too slow, you could identify the menus without submenus on the server side and give them a class, something like menu_no_submenu, so you could then replace the complex selector in the above code with much faster '.menu_no_submenu' or, even better, do the whole arrow hiding thing in css.

孤凫 2024-12-29 11:33:25

您可以在模板代码中设置背景,而不是使用 jQuery。

创建一个包含背景的 css 类。

.menuHeader { background: url("themes/tutorial/images/menuarrowdown.gif") no-repeat scroll right center transparent; }

然后,如果菜单有子菜单,则在模板代码中将类设置为 h3。

<% control Menu(2) %>
    <% if Children %>   
      <h3 class="menuHeader"><a href="$Link" title="Go to the "{$Title}" page">$MenuTitle</a></h3>
    <ul class="second_level">
          <% control Children %>
            <li class="$LinkingMode"><a href="$Link" title="Go to the "{$Title}" page">$MenuTitle</a></li>
          <% end_control %>
    </ul>
   <% else %>
        <h3><a href="$Link" title="Go to the "{$Title}" page">$MenuTitle</a></h3>
   <% end_if %><% end_control %>

You could set the background in the template code rather than using jQuery.

Make a css class that contains the background.

.menuHeader { background: url("themes/tutorial/images/menuarrowdown.gif") no-repeat scroll right center transparent; }

Then in your template code set the class to the h3 if the menu has children.

<% control Menu(2) %>
    <% if Children %>   
      <h3 class="menuHeader"><a href="$Link" title="Go to the "{$Title}" page">$MenuTitle</a></h3>
    <ul class="second_level">
          <% control Children %>
            <li class="$LinkingMode"><a href="$Link" title="Go to the "{$Title}" page">$MenuTitle</a></li>
          <% end_control %>
    </ul>
   <% else %>
        <h3><a href="$Link" title="Go to the "{$Title}" page">$MenuTitle</a></h3>
   <% end_if %><% end_control %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文