在ul和li标签中添加和删除类时遇到问题

时间:2019-06-13 13:02:12

标签: javascript jquery

我正在使用Jquery开发应用程序,我想有条件地添加类和删除类。在下面的代码中有菜单和子菜单。

如果用户单击#two#three(以打开子菜单),则将子菜单类添加到包含ul#two的{​​{1}}标记中,并菜单类将被删除。

如果用户单击#three#three(在#four上),则菜单类将添加到包含li和{{ 1}},并将子菜单类从包含ul#three

#four中删除

ul

2 个答案:

答案 0 :(得分:1)


在单击按钮上找到属于 subMenu 的类,如果存在该类,则更改为 iconadd 的类,或者不删除 iconadd

{"Message":"An error has occurred.",
"ExceptionMessage":"No log found for 'latest'.",
"ExceptionType":"System.IO.FileNotFoundException",
"StackTrace":"
    at Kudu.Core.Deployment.DeploymentManager.GetLogEntries(String id) in C:\\Kudu Files\\Private\\src\\master\\Kudu.Core\\Deployment\\DeploymentManager.cs:line 95\r\n
   at Kudu.Services.Deployment.DeploymentController.GetLogEntry(String id) in C:\\Kudu Files\\Private\\src\\master\\Kudu.Services\\Deployment\\DeploymentController.cs:line 375"
}

谢谢

答案 1 :(得分:0)

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
    $(document).ready(function(){
      $(".left_pannel ul li ul").click(function(){
        if ($(this).hasClass('submenu')) {      
          $(".left_pannel ul li").addClass("iconadd");
        } else {
          $(".left_pannel ul li").removeClass("iconadd");
        }
      });
    });
</script>

<body>
  <div class="left_pannel">
    <ul>
      <li><a href="#one" class="achore">Matches</a>
        <ul id="one" class="submenu" style="display: none;">
          <li><a href="https://www.google.com">Add Matches(m)</a></li>
          <li><a href="https://www.google.com">Add Cricket(m)</a></li>
        </ul>
      </li>
      <li><a href="#two" class="achore">Quize Master</a>
        <ul id="two" class="submenu" style="display: none;">
          <li><a href="">Add Matches(m)</a></li>
          <li><a href="">Add Cricket(m)</a></li>

        </ul>
      </li>
      <li id="#three"><a href="https://www.google.com">Excel Update</a></li>
      <li id="#four"><a href="#">Application version</a></li>
    </ul>
  </div>
</body>

尝试此代码