下拉菜单不允许我单击菜单项

时间:2019-04-29 14:22:47

标签: javascript jquery html css

我的网站上有一个下拉菜单,除了不能单击链接之外,它都可以正常运行。每当我单击链接(在下拉列表中)时,它只会关闭下拉列表,而不会重定向到页面。我试着玩了一下代码,但是并没有解决它。任何帮助表示赞赏。

jQuery(document).ready(function() {
    // insert toggle button
    jQuery(".advanced-sidebar-nav").each(function() {
        jQuery(".menu-item-has-children", jQuery(this)).each(function() {
            // insert toggle button
            jQuery("> a", jQuery(this)).append(
                '<span class="advanced-sidebar-nav-toggle"></span>'
            );

            // add indent
            var depth = jQuery(this).parents(".menu-item-has-children").length;
            jQuery("ul li a", jQuery(this)).attr(
                "style",
                "padding-left:" + (depth + 2) * 20 + "px !important"
            );

            // open nav menu toggle
            if (jQuery("ul", jQuery(this)).css("display") == "block") {
                jQuery(".advanced-sidebar-nav-toggle", jQuery(this)).addClass(
                    "advanced-sidebar-nav-toggle-open"
                );
            }
        });
    });
    jQuery(".advanced-sidebar-nav").on(
        "click",
        ".dropdown",
        function(e) {
            e.preventDefault();

            
           const self = $(this).find(".advanced-sidebar-nav-toggle");

            jQuery(".advanced-sidebar-nav-menu-open").each(
              function() {
                  $(this).find(".advanced-sidebar-nav-toggle").parent("a").removeClass("advanced-sidebar-nav-menu-open").siblings("ul").slideUp(300);
                  $(this).find(".advanced-sidebar-nav-toggle").not(self).removeClass("advanced-sidebar-nav-toggle-open");
              }
            );

            jQuery(self).toggleClass("advanced-sidebar-nav-toggle-open");

            if(jQuery(self).hasClass("advanced-sidebar-nav-toggle-open")) {
                jQuery(self)
                      .parent("a")
                      .addClass("advanced-sidebar-nav-menu-open")
                      .siblings("ul")
                      .slideDown(300);
                      
                  } else {
                    jQuery(self)
                      .parent("a")
                      .removeClass("advanced-sidebar-nav-menu-open")
                      .siblings("ul")
                      .slideUp(300);
                  }
        }
    );
    
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="home-category-container">
			<aside id="advanced_sidebar_nav_widget-3" class="widget-odd widget-last widget-first widget-1 widget woocommerce widget_product_categories widget advanced-sidebar-nav-widget"><h3 class="widget-title">Product categorieën</h3><div class="advanced-sidebar-nav advanced-sidebar-nav-default"><ul id="menu-sidebar-menu" class="advanced-sidebar-menu"><li id="menu-item-4701" class="dropdown menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-4701"><a href="#">Laptops<span class="advanced-sidebar-nav-toggle"></span></a>
<ul class="sub-menu">
	<li id="menu-item-4696" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4696"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/laptops/i3/" style="padding-left:40px !important">i3</a></li>
	<li id="menu-item-4697" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4697"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/laptops/i5/" style="padding-left:40px !important">i5</a></li>
	<li id="menu-item-4698" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4698"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/laptops/i7/" style="padding-left:40px !important">i7</a></li>
	<li id="menu-item-4695" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4695"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/laptops/game-laptops/" style="padding-left:40px !important">Game laptops</a></li>
</ul>
</li>
<li id="menu-item-4703" class="dropdown menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-4703"><a href="#">Computers<span class="advanced-sidebar-nav-toggle"></span></a>
<ul class="sub-menu">
	<li id="menu-item-4709" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4709"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/computers/tower/" style="padding-left:40px !important">Tower</a></li>
	<li id="menu-item-4710" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4710"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/computers/workstation/" style="padding-left:40px !important">Workstation</a></li>
	<li id="menu-item-4708" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4708"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/computers/game-computers/" style="padding-left:40px !important">Game computers</a></li>
</ul>
</li>
<li id="menu-item-4704" class="dropdown menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-4704"><a href="#">Monitoren<span class="advanced-sidebar-nav-toggle"></span></a>
<ul class="sub-menu">
	<li id="menu-item-4712" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4712"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/monitoren/19/" style="padding-left:40px !important">19″</a></li>
	<li id="menu-item-4713" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4713"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/monitoren/22/" style="padding-left:40px !important">22″</a></li>
	<li id="menu-item-4714" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4714"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/monitoren/24/" style="padding-left:40px !important">24″</a></li>
	<li id="menu-item-4715" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4715"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/monitoren/27/" style="padding-left:40px !important">27″</a></li>
	<li id="menu-item-4716" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4716"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/monitoren/32/" style="padding-left:40px !important">32″</a></li>
</ul>
</li>
<li id="menu-item-4705" class="dropdown menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-4705"><a href="#">Netwerkapparatuur<span class="advanced-sidebar-nav-toggle"></span></a>
<ul class="sub-menu">
	<li id="menu-item-4721" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4721"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/netwerkapparatuur/switchers/" style="padding-left:40px !important">Switchers</a></li>
	<li id="menu-item-4718" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4718"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/netwerkapparatuur/nas/" style="padding-left:40px !important">Nas</a></li>
	<li id="menu-item-4722" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4722"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/netwerkapparatuur/thin-clients/" style="padding-left:40px !important">Thin clients</a></li>
	<li id="menu-item-4717" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4717"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/netwerkapparatuur/accespoints/" style="padding-left:40px !important">Access Points</a></li>
	<li id="menu-item-4719" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4719"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/netwerkapparatuur/repeaters/" style="padding-left:40px !important">Repeaters</a></li>
	<li id="menu-item-4720" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4720"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/netwerkapparatuur/routers/" style="padding-left:40px !important">Routers</a></li>
</ul>
</li>
<li id="menu-item-4706" class="dropdown menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-4706"><a href="#">Hard Drive<span class="advanced-sidebar-nav-toggle"></span></a>
<ul class="sub-menu">
	<li id="menu-item-4725" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4725"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/hard-drive/ssd-drive/" style="padding-left:40px !important">SSD Drive</a></li>
	<li id="menu-item-4726" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4726"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/hard-drive/ssd-m-2/" style="padding-left:40px !important">SSD M.2</a></li>
	<li id="menu-item-4727" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4727"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/hard-drive/ssd-msata/" style="padding-left:40px !important">SSD Msata</a></li>
	<li id="menu-item-4723" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4723"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/hard-drive/sata-2-5/" style="padding-left:40px !important">Sata 2.5″</a></li>
	<li id="menu-item-4724" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4724"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/hard-drive/sata-3-5/" style="padding-left:40px !important">Sata 3.5″</a></li>
</ul>
</li>
<li id="menu-item-4707" class="dropdown menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-4707"><a href="#">Accessoires<span class="advanced-sidebar-nav-toggle"></span></a>
<ul class="sub-menu">
	<li id="menu-item-4729" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4729"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/accessoires/kabels/" style="padding-left:40px !important">Kabels</a></li>
	<li id="menu-item-4728" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4728"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/accessoires/adapters/" style="padding-left:40px !important">Adapters</a></li>
	<li id="menu-item-4730" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4730"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/accessoires/memory/" style="padding-left:40px !important">Memory</a></li>
</ul>
</li>
</ul></div></aside>	


		</div>

1 个答案:

答案 0 :(得分:2)

请检查此内容。我仅将您的目标点.dropdown更改为.dropdown > a,但我不知道为什么使用非常困难的方法来实现这一目标。

jQuery(document).ready(function() {
    // insert toggle button
    jQuery(".advanced-sidebar-nav").each(function() {
        jQuery(".menu-item-has-children", jQuery(this)).each(function() {
            // insert toggle button
            jQuery("> a", jQuery(this)).append(
                '<span class="advanced-sidebar-nav-toggle"></span>'
            );

            // add indent
            var depth = jQuery(this).parents(".menu-item-has-children").length;
            jQuery("ul li a", jQuery(this)).attr(
                "style",
                "padding-left:" + (depth + 2) * 20 + "px !important"
            );

            // open nav menu toggle
            if (jQuery("ul", jQuery(this)).css("display") == "block") {
                jQuery(".advanced-sidebar-nav-toggle", jQuery(this)).addClass(
                    "advanced-sidebar-nav-toggle-open"
                );
            }
        });
    });
    jQuery(".advanced-sidebar-nav").on(
        "click",
        ".dropdown > a",
        function(e) {
            e.preventDefault();

            
           const self = $(this).find(".advanced-sidebar-nav-toggle");

            jQuery(".advanced-sidebar-nav-menu-open").each(
              function() {
                  $(this).find(".advanced-sidebar-nav-toggle").parent("a").removeClass("advanced-sidebar-nav-menu-open").siblings("ul").slideUp(300);
                  $(this).find(".advanced-sidebar-nav-toggle").not(self).removeClass("advanced-sidebar-nav-toggle-open");
              }
            );

            jQuery(self).toggleClass("advanced-sidebar-nav-toggle-open");

            if(jQuery(self).hasClass("advanced-sidebar-nav-toggle-open")) {
                jQuery(self)
                      .parent("a")
                      .addClass("advanced-sidebar-nav-menu-open")
                      .siblings("ul")
                      .slideDown(300);
                      
                  } else {
                    jQuery(self)
                      .parent("a")
                      .removeClass("advanced-sidebar-nav-menu-open")
                      .siblings("ul")
                      .slideUp(300);
                  }
        }
    );
    
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="home-category-container">
			<aside id="advanced_sidebar_nav_widget-3" class="widget-odd widget-last widget-first widget-1 widget woocommerce widget_product_categories widget advanced-sidebar-nav-widget"><h3 class="widget-title">Product categorieën</h3><div class="advanced-sidebar-nav advanced-sidebar-nav-default"><ul id="menu-sidebar-menu" class="advanced-sidebar-menu"><li id="menu-item-4701" class="dropdown menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-4701"><a href="#">Laptops<span class="advanced-sidebar-nav-toggle"></span></a>
<ul class="sub-menu">
	<li id="menu-item-4696" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4696"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/laptops/i3/" style="padding-left:40px !important">i3</a></li>
	<li id="menu-item-4697" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4697"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/laptops/i5/" style="padding-left:40px !important">i5</a></li>
	<li id="menu-item-4698" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4698"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/laptops/i7/" style="padding-left:40px !important">i7</a></li>
	<li id="menu-item-4695" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4695"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/laptops/game-laptops/" style="padding-left:40px !important">Game laptops</a></li>
</ul>
</li>
<li id="menu-item-4703" class="dropdown menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-4703"><a href="#">Computers<span class="advanced-sidebar-nav-toggle"></span></a>
<ul class="sub-menu">
	<li id="menu-item-4709" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4709"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/computers/tower/" style="padding-left:40px !important">Tower</a></li>
	<li id="menu-item-4710" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4710"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/computers/workstation/" style="padding-left:40px !important">Workstation</a></li>
	<li id="menu-item-4708" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4708"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/computers/game-computers/" style="padding-left:40px !important">Game computers</a></li>
</ul>
</li>
<li id="menu-item-4704" class="dropdown menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-4704"><a href="#">Monitoren<span class="advanced-sidebar-nav-toggle"></span></a>
<ul class="sub-menu">
	<li id="menu-item-4712" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4712"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/monitoren/19/" style="padding-left:40px !important">19″</a></li>
	<li id="menu-item-4713" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4713"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/monitoren/22/" style="padding-left:40px !important">22″</a></li>
	<li id="menu-item-4714" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4714"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/monitoren/24/" style="padding-left:40px !important">24″</a></li>
	<li id="menu-item-4715" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4715"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/monitoren/27/" style="padding-left:40px !important">27″</a></li>
	<li id="menu-item-4716" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4716"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/monitoren/32/" style="padding-left:40px !important">32″</a></li>
</ul>
</li>
<li id="menu-item-4705" class="dropdown menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-4705"><a href="#">Netwerkapparatuur<span class="advanced-sidebar-nav-toggle"></span></a>
<ul class="sub-menu">
	<li id="menu-item-4721" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4721"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/netwerkapparatuur/switchers/" style="padding-left:40px !important">Switchers</a></li>
	<li id="menu-item-4718" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4718"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/netwerkapparatuur/nas/" style="padding-left:40px !important">Nas</a></li>
	<li id="menu-item-4722" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4722"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/netwerkapparatuur/thin-clients/" style="padding-left:40px !important">Thin clients</a></li>
	<li id="menu-item-4717" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4717"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/netwerkapparatuur/accespoints/" style="padding-left:40px !important">Access Points</a></li>
	<li id="menu-item-4719" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4719"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/netwerkapparatuur/repeaters/" style="padding-left:40px !important">Repeaters</a></li>
	<li id="menu-item-4720" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4720"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/netwerkapparatuur/routers/" style="padding-left:40px !important">Routers</a></li>
</ul>
</li>
<li id="menu-item-4706" class="dropdown menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-4706"><a href="#">Hard Drive<span class="advanced-sidebar-nav-toggle"></span></a>
<ul class="sub-menu">
	<li id="menu-item-4725" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4725"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/hard-drive/ssd-drive/" style="padding-left:40px !important">SSD Drive</a></li>
	<li id="menu-item-4726" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4726"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/hard-drive/ssd-m-2/" style="padding-left:40px !important">SSD M.2</a></li>
	<li id="menu-item-4727" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4727"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/hard-drive/ssd-msata/" style="padding-left:40px !important">SSD Msata</a></li>
	<li id="menu-item-4723" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4723"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/hard-drive/sata-2-5/" style="padding-left:40px !important">Sata 2.5″</a></li>
	<li id="menu-item-4724" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4724"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/hard-drive/sata-3-5/" style="padding-left:40px !important">Sata 3.5″</a></li>
</ul>
</li>
<li id="menu-item-4707" class="dropdown menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-4707"><a href="#">Accessoires<span class="advanced-sidebar-nav-toggle"></span></a>
<ul class="sub-menu">
	<li id="menu-item-4729" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4729"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/accessoires/kabels/" style="padding-left:40px !important">Kabels</a></li>
	<li id="menu-item-4728" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4728"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/accessoires/adapters/" style="padding-left:40px !important">Adapters</a></li>
	<li id="menu-item-4730" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-4730"><a href="http://projecten.jumsum.nl/adtcomputers/product-category/accessoires/memory/" style="padding-left:40px !important">Memory</a></li>
</ul>
</li>
</ul></div></aside>	


		</div>