简体   繁体   English

使用jquery将单级转换为多级下拉菜单

[英]Converting single level to multilevel dropdown menu using jquery

I just developed a single level dropdown menu using jquery... How to refactor the same code to make it a multilevel dropdown menu... 我刚刚开发了一个使用jquery的单级下拉菜单...如何重构相同的代码,使其成为一个多级下拉菜单......

Here is my working sample .... 这是我的工作样本 ....

The jquery what i used, 我用过的jquery,

$(document).ready(function() {
$("#Programming ul.child").removeClass("child");
$("#Programming li").has("ul").hover(function() {
   $(this).addClass("current").children("ul").fadeIn();
     }, function() {
    $(this).removeClass("current").children("ul").stop(true, true).css("display",
                                                                     "none");
                              });
                            });
​

EDIT: 编辑:

Menu1       Menu2
 SubMenu1    SubMenu1
 Submenu2    SubMenu2
    SubMenu21       
    SubMenu22

The second submenu level can be anywere... 第二个子菜单级别可以是任何...

As your code is very generic about hiding/showing the child of the list, all I did was nested another UL inside an LI element, and then positioned it according to it's parent: 由于你的代码在隐藏/显示列表的子代码方面非常通用,我所做的只是在LI元素中嵌套另一个UL,然后根据它的父元素定位它:

http://jsbin.com/oteze/5 http://jsbin.com/oteze/5

(No JS changes, just a new line of CSS targeting Third Level Menu items) (没有JS更改,只是针对三级菜单项的新CSS系列)

#Programming li ul li ul { left:100px;top:0px; }

Is there any reason why you wouldn't want to use the existing jquery plugin dedicated to this? 你有什么理由不想使用现有的jquery插件吗?

A good one is: http://users.tpg.com.au/j_birch/plugins/superfish/ 一个好的是: http//users.tpg.com.au/j_birch/plugins/superfish/

Suckerfish has been around for years as the pure JS version that's about as solid as you can get. Suckerfish已经存在多年了,因为纯粹的JS版本就像你可以得到的那样坚固。

Even if you don't use this I'm sure it'd be helpful to check out his source. 即使你不使用它,我也相信检查他的来源是有帮助的。

Here is my solution: http://jsbin.com/oteze/9 I tested it on Firefox 3.6.8. 这是我的解决方案: http//jsbin.com/oteze/9我在Firefox 3.6.8上测试过它。 ADD: Now it works in IE7 too. ADD:现在它也适用于IE7。

You can nest any number of submenus anywhere. 您可以在任何地方嵌套任意数量的子菜单。 Just like that: 就像那样:

<li><a href="#">Child 1.2</a>
   <ul class="child">
       <li><a href="#">Child 1.2.1</a></li>
       <li><a href="#">Child 1.2.2</a></li>
   </ul>                                                      
</li>

I modified your code a little, so there is difference between first level submenu and other levels. 我稍微修改了你的代码,所以第一级子菜单和其他级别之间存在差异。 I also moved all show/hide logic to JS. 我还将所有显示/隐藏逻辑移动到JS。

   $(document).ready(function() {

        $("#Programming ul.child-first-level").hide();
        $("#Programming ul.child").hide();

        $("#Programming ul.child").each(function(index) {
            $(this).css("margin-left", $(this).parent().css("width"));
        });


        $("#Programming li ul.child-first-level").parent().hover(
            function() {
                $(this).children("ul").fadeIn();
            },
            function() {
                $(this).children("ul").stop(true, true).css("display", "none");
            }
        );

        $("#Programming li ul.child").parent().hover(
            function() {
                var offset = $(this).children("ul").offset();
                offset.top = 0;
                $(this).children("ul").offset(offset);
                $(this).children("ul").css("margin-left", $(this).parent().css("width"));
                $(this).children("ul").fadeIn();
            },
            function() {
                $(this).children("ul").stop(true, true).css("display", "none");
            }
        );

   });

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

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