简体   繁体   English

jQuery Internet Explorer / Edge toggleClass问题

[英]Jquery Internet Explorer/Edge toggleClass issue

So I made this horizontal menu which has a div slide from underneath it when clicked. 因此,我制作了此水平菜单,单击该菜单时,其下方有一个div幻灯片。 Everything works fine, except Internet Explorer and Edge fold out all submenus when any of the items with a submenu is clicked. 一切正常,除了单击带有子菜单的项目时,Internet Explorer和Edge会折叠所有子菜单。 I have seen that there are can be some compatibility issues with toggle()/toggleClass() but have not found a solution that worked yet. 我已经看到toggle()/ toggleClass()可能存在一些兼容性问题,但尚未找到有效的解决方案。 Any ideas? 有任何想法吗?

Js: Js:

 $(document).ready(function (){
        $('.info-menu').click(function(){
            // Append proper class to generic submenu name.
            var menuNumber = this.classList[1];
            var menuToggle = '.top-sub-nav-fixed.' + menuNumber;
            // If other sub menu item is active, toggle active
            $('.top-sub-nav-fixed').each(function (){
               if (typeof this.classList[2] != 'undefined' && this.classList[1] != menuNumber ) {
                   $(this).toggleClass('active');
               }
            });
            // Toggle appropriate menu item
            $(menuToggle).toggleClass('active');
        });
    });

Fiddle here: JSFIddle 小提琴在这里: JSFIddle

The solution was pretty straightforward: instead of using toggleClass() inside of the each() statement using removeClass() did the trick. 该解决方案是非常简单的:而不是使用toggleClass()内的each() using语句removeClass()的伎俩。 For some reason IE sets all DIVs to active (as opposed to webkit or gecko based browsers) when using toggleClass() in this function. 由于某些原因,当在此函数中使用toggleClass()时,IE会将所有DIV设置为活动状态(与基于Webkit或gecko的浏览器相反toggleClass()

$(document).ready(function (){
    $('.info-menu').click(function(){
        // Append proper class to generic submenu name.
        var menuNumber = this.classList[1];
        var menuToggle = '.top-sub-nav-fixed.' + menuNumber;
        // If other sub menu item is active, toggle active
        $('.top-sub-nav-fixed').each(function (){
           if (typeof this.classList[2] != 'undefined' && this.classList[1] != menuNumber ) {
               $(this).removeClass('active');
           }
        });
        // Toggle appropriate menu item
        $(menuToggle).toggleClass('active');
    });
});

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

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