简体   繁体   English

使用javascript将子菜单添加到垂直菜单

[英]Add sub-menu to vertical menu using javascript

I managed to design a vertical menu using javascript, but I'm failing horribly when trying to add a submenu to it. 我设法使用javascript设计了一个垂直菜单,但是尝试向其中添加子菜单时却失败了。

Heres the example http://www.bootply.com/isPSKNdGCG# 以下是示例http://www.bootply.com/isPSKNdGCG#

Can anyone give me a hint on how to add the submenu to be opened under the first level? 谁能给我一个关于如何添加要在第一级下打开的子菜单的提示吗?

Thanks. 谢谢。

-- -

I managed to make it work through this jquery code, not sure if this is the best practice, but worked well. 我设法通过此jquery代码使其工作,不确定这是否是最佳实践,但效果很好。

  • Relatório Relatório

     function toggle_visibility(id) { var e = document.getElementById(id); if (e.style.display == 'block') { e.style.display = 'none'; } else { e.style.display = 'block'; } } 
     <li> <div class="dl_parent"> <span onclick="toggle_visibility('dl_sub_dd');"> <a href="#">Relatório <span class="caret" style="margin-left: 10px;"></span></a> </span> <div id="dl_sub_dd"> <ul> <li><a href="#">Secretaria</a></li> <li><a href="#">Tesouraria</a></li> <li><a href="#">Tesouraria</a></li> <li><a href="#">Chancelaria</a></li> </ul> </div> </li> 

  • Primary CSS Menu Principles CSS基本菜单原则

    The classic solution is to modify either the CSS display or the CSS visibility attribute on a div. 经典的解决方案是修改div上的CSS显示或CSS可见性属性。 The later of these two will require a non-transparent background and a higher Z-index the menu panes. 这两个中的较晚者将需要不透明的背景和较高的Z索引菜单窗格。 Your example code uses the first of the two, which is slower and not quite as pretty in most designs. 您的示例代码使用了两者中的第一个,它比较慢,并且在大多数设计中不太美观。

    For a nested menu, one simply nests the div-ul-li-button hierarchy inside the menu pane li of the parent. 对于嵌套菜单,只需将div-ul-li-button层次结构嵌套在父级菜单窗格li中。

    <button onwhatever=...> <!-- optional -->
    <div class='parent-menu'>
      <ul>
        <li>
          <button onwhatever=...>
          <div id=...>
            <ul>
              <li>
                <button onwhatever=...>
                <div id=...>
                  <ul>
                    <li>
                      <button onclick=...>
    

    The sub-menu expands just like its parent, except you have to give the panes ids so you can associate user events with the sub-menu they intend to expand. 子菜单的扩展与它的父菜单一样,只是您必须提供窗格ID,以便可以将用户事件与他们打算扩展的子菜单相关联。 (For grandchild panes, you need to use distinct child and grandchild ids, usually following a simple pattern.) (对于孙面板,您需要使用不同的子ID和孙ID,通常遵循简单的模式。)

    It is better HTML5 structural design to use button tags instead of input tags or reusing the div tag for both ul container and button. 更好的HTML5结构设计是使用按钮标签代替输入标签,或者对ul容器和按钮重新使用div标签。 If you go with this cleaner and more readable structure, use the buttons in the panes too, for consistency. 如果您使用这种更清洁,更易读的结构,请也使用窗格中的按钮以保持一致性。 This way you are always dealing with button events to drive your expansion, contraction, and final menu selection actions. 这样,您将始终处理按钮事件以驱动扩展,收缩和最终菜单选择操作。

    Cascading Style Design 级联样式设计

    You only need one CSS class. 您只需要一个CSS类。 Everything else can be CSS selected relative to that div.parent-menu base. 相对于div.parent-menu基础,可以选择所有其他内容。 How you use block, inline, or other layouts depends largely on your page design and may switch in real time based on the browser's view port width. 块,行或其他布局的使用方式在很大程度上取决于您的页面设计,并可能根据浏览器的视图端口宽度实时切换。 Also responsive page designs often change the menu layout based on media type. 同样,响应式页面设计通常会根据媒体类型更改菜单布局。

    Get your mechanics working first, and then tweak your style as you test on multiple devices and vary your desktop window width. 首先让您的机械师工作,然后在多种设备上进行测试并调整桌面窗口宽度时调整样式。

    Choosing and Associating Your Events 选择和关联事件

    Depending on your desired behavior, you would use onclick or onmouseover to trigger the appropriate changes. 根据所需的行为,您可以使用onclick或onmouseover触发适当的更改。 The mouse over event is fast, but sometimes too fast for the user if the layout is compact. 鼠标悬停事件很快,但是如果布局紧凑,对于用户来说有时太快了。

    Graceful Show and Hide Mechanics 优美的表演和隐藏机制

    In responsive design, you can get tricky in how the menu and submenu expand based on the width of the display or the media type. 在响应式设计中,您可能会难以掌握菜单和子菜单如何根据显示的宽度或媒体类型进行扩展。

    There are ways to slow the mechanics of showing or expanding or the speed at which writing appears or fades using third degree polynomials. 有多种方法可以使用三阶多项式来降低显示或扩展的机制或书写出现或消失的速度。 Most of the library components that people love to plug in, aren't quite that sophisticated. 人们喜欢插入的大多数库组件并不十分复杂。 They sometimes provide linear first degree mechanics. 他们有时会提供线性一级学位机制。 Nonetheless, many like them because the provide the basics right out of the box, cross-browser tested, and responsive designed. 但是,许多人还是喜欢它们,因为ISP提供了开箱即用的基础知识,经过跨浏览器测试和快速响应的设计。

    Usually, only a few lines of JavaScript will provide mechanical grace, and it saves the additional HTTP activity and keeps your code highly visible, especially if you name your functions and variables at least as well as a high school graduate. 通常,只有几行JavaScript会提供机械的宽限期,并且可以节省额外的HTTP活动并保持代码的高度可见性,尤其是在您至少命名函数和变量以及高中毕业生的情况下。

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM