简体   繁体   English

在 JavaScript 中单击另一个手风琴时删除显示手风琴

[英]Remove show accordion when click on another accordion in JavaScript

Visit this website: http://dev.giftsdesign.com.sg/product-list/0/0访问此网站: http : //dev.giftsdesign.com.sg/product-list/0/0

I will expect the output like, If I click one sidebar menu, others sidebar menus should be collapsed.我会期待这样的输出,如果我点击一个侧边栏菜单,其他侧边栏菜单应该被折叠。

Before Click: <ul class="sidebar-submenu" style="display: none;">点击前: <ul class="sidebar-submenu" style="display: none;">

After Click: <ul class="sidebar-submenu show" style="display: block;">点击后: <ul class="sidebar-submenu show" style="display: block;">

I think this is code for that,我认为这是代码,

$(document).ready(function() {

    $('.sidebar-link').click(function(e) {
        e.preventDefault();

        var $this = $(this);

        if ($this.next().hasClass('show')) {
            $this.next().removeClass('show');
            $this.next().slideUp(350);
        } else {
            $this.parent().parent().find('sidebar-submenu-item .sidebar-submenu').removeClass('show');
            $this.parent().parent().find('sidebar-submenu-item .sidebar-submenu').slideUp(350);
            $this.next().toggleClass('show');
            $this.next().slideToggle(350);
        }
    });

How to modify to get expected output.如何修改以获得预期的输出。 Please help me!请帮我!

Try this:尝试这个:

$('.sidebar-link').click(function (e) {
  e.preventDefault();

  $('.show').removeClass("show");
  $('.show').css("style","none");
  ......
  
});

try this:尝试这个:

$(document).ready(function() {

    $('.sidebar-link').click(function(e) {
        e.preventDefault();

        var $this = $(this);
      document.querySelectorAll('.sidebar-link').forEach(el=> {
        el.classList.remove('show')
      });
e.classList.add('show');
            $this.parent().parent().find('sidebar-submenu-item .sidebar-submenu').removeClass('show');
            $this.parent().parent().find('sidebar-submenu-item .sidebar-submenu').slideUp(350);
            $this.next().toggleClass('show');
            $this.next().slideToggle(350);
        
    });

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

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