简体   繁体   English

在else子句中停止阻止默认

[英]Stop Prevent Default in else clause

Hello I have a mobile nav where some items have a subnav so I have set a preventDefault to stop these doing there normal actions and open the sub menu but some don't have a submenu ad now these don't work as normal. 您好,我有一个移动导航,其中某些项具有子导航,因此我设置了preventDefault来阻止这些项执行正常操作并打开子菜单,但是有些项没有子菜单广告,现在这些项将无法正常工作。

Any help would be great here is my code. 任何帮助都会很棒,这是我的代码。 Also this is a mobile only problem. 这也是一个仅移动的问题。

 $('.touch .mobile-list > li > a').on('click', function(e){
            e.preventDefault();
            if($(this).parents('li').hasClass('visible-submenu')) {
                $(this).parents('li').removeClass('visible-submenu');

            } else {
                 $('.mobile-list li.visible-submenu').removeClass('visible-submenu');
                 $(this).parents('li').addClass('visible-submenu');
            }

        });

Try moving e.preventDefault(); 尝试移动e.preventDefault(); into the appropriate branch of the if statement 进入if语句的适当分支

for example: 例如:

$('.touch .mobile-list > li > a').on('click', function(e){
        if($(this).parents('li').hasClass('visible-submenu')) {
            $(this).parents('li').removeClass('visible-submenu');
            e.preventDefault();
        } else {
             $('.mobile-list li.visible-submenu').removeClass('visible-submenu');
             $(this).parents('li').addClass('visible-submenu');
        }

    });

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

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