首页 > 解决方案 > 显示和隐藏

问题描述

 let opened = false;
 function turnNestedlist(opened)
 {
     elem = countImg;
   $(`.list__ul .nested-list.num-${elem}`).click(function(event)
    {
      if (!opened){
        $(this).addClass('active');
        $(`.list__ul .nested-list.num-${elem}.active li`).show("fast");
        opened = true;
      }
      $(this).click(function(event)
      {
        if (opened){
         $(this).removeClass('active');
         $(`.list__ul .nested-list.num-${elem} li`).hide("fast");
         opened = false;
        }
      });
    });
  }
  $(`.list__ul .nested-list.num-${countImg}`).click(turnNestedlist(opened));

任务是,当您单击每个列表时ul,会打开该元素的 4 个嵌套元素li。此代码有效,但对于每个ul元素仅一次,即:第一次单击发生 - 列表关闭,第二次单击打开,第三次单击 - 列表立即打开和关闭。

1

标签: jquerycss

解决方案


推荐阅读