简体   繁体   English

使用ul li的二级菜单项不起作用

[英]Second level menu item using ul li not working

I have a collapsible ul li . 我有一个可折叠的ul li

It works fine for the first level, but it is not expanding the ul elements inside the li (second level menu). 它在第一级工作正常,但没有扩展li (第二级菜单)内部的ul元素。

This is the code: 这是代码:

$(document).ready(function () {
    $("li").slideUp();
});

var hovering = 0;
var element;
$("ul,li").mouseenter(function(e) {
    setTimeout(function() { clearHover(); }, 1000);
    if (hovering == 0)
    {
        hovering = 1;
        $("#testdiv").css({ "background-color": "red" });
        $(this).find("> li").delay(500).slideDown("medium");
        $(".nav").not(this).find("li").delay(500).slideUp();
    }
});

function clearHover() {
    hovering = 0;
    $("#testdiv").css({ "background-color": "green" });
};

You can check the fiddle here 你可以在这里检查小提琴

I tried to simplify the code using show, hide 我试图使用show, hide来简化代码

 $(document).ready(function () {
     $("li").slideUp();
     // setTimeout(function(){ $("ul li").slideDown(); },1000);
 });


 $("ul").hover(function (e) {
     $(this).children().stop(true,true).show();
     $(".nav").not(this).find("li").stop(true,true).hide();
});

 $("li").hover(function (e) {
     if($(this).children().length>0){
     $(this).children().show();
         $(this).children("ul").css({"z-index":"100"});
     }
     //console.log($(this).children().length+ " " + Math.random);

});

Please check this fiddle 请检查这个小提琴

If you seek to expand the whole 3rd level once on mouse over on the level 2, you need to add a parent selector; 如果您希望在第2级上的鼠标悬停一次时扩展整个第3级,则需要添加一个父选择器; you are just telling to show the "li" but inside this li you have "ul". 您只是要显示“ li”,但在此li内您有“ ul”。 Therefor, this selector should work: 因此,此选择器应工作:

  $(this).find("ul > li").delay(500).slideDown("medium");

The whole code: 整个代码:

  $(document).ready(function () {
      $("li").slideUp();
  });

 var hovering=0;
 var element;
 $("ul,li").mouseenter(function (e) {
setTimeout(function(){clearHover();},1000);
if(hovering==0)
{
     hovering=1;
    $("#testdiv").css({"background-color":"red"});
     $(this).find("> li").delay(500).slideDown("medium");
     $(this).find("ul > li").delay(500).slideDown("medium");


     $(".nav").not(this).find("li").delay(500).slideUp();
     $(".nav").not(this).find("li ul").delay(500).slideUp();
   }
 });


function clearHover(){

hovering=0;
    $("#testdiv").css({"background-color":"green"});
};

And the fiddle 小提琴

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

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