IE8 toggleClass not working properly

断了今生、忘了曾经 提交于 2019-12-10 22:19:30

问题


Well I have a menu and there I want to have an class called active, when its active, obviously.

To toggle this class I am using the jQuery toggle function, but it does just activate it in the js element. If I check the dom element, this class is never set. But I need this class, because of my CSS styles.

I know that there are already many topics about toggleClass jQuery, but they doesn't fit (at least I couldn't find anything suitable)

Here some code:

$('#menu').on('click', '.tab span', function (e) {
    e.stopPropagation();
    var $tab = $(this).parent('.tab');
    $tab.siblings('.tab.active').removeClass('active');
    $tab.toggleClass('active');
});

HTML markup

<div id="menu">
    <div class="tab">
        <span></span>
    </div>
</div>

Its working in all browsers, except IE8 Thanks in advance.


回答1:


Try this instead:

$('#menu').on('click', '.tab span', function (e) {
    e.stopPropagation();
    var $tab = $(this).parent('.tab');
    $tab.addClass('active').siblings('.tab').removeClass('active');
});



回答2:


Try this:

$('#menu').on('click', '.tab span', function (e) {
    e.stopPropagation();
    var $tab = $(this).parent('.tab');

    // remove active from all tabs
    $('#menu .tab').removeClass('active');

    // make current tab as active
    $tab.addClass('active');
});


来源:https://stackoverflow.com/questions/16671917/ie8-toggleclass-not-working-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!