简体   繁体   中英

jquery addClass() not working after removeClass()

I have got two jquery functions. When calling the first function classes are being removed and two onclick attributes are being changed.

But after calling the function to remove the classes the second function won't work, so the classes cannot be added anymore and the onclick event, too.

Edit 1: As already said the first function is working! The onclick events are being changed and the classes are being removed. It does not work when adding the same classes and functions again within the second function! When adding a new class it is working, but it is not working when adding one of the removed classes or onclick events!

Here's the code:

// show info
function showKursInfo() {
    $(".kright").removeClass("kursMobileOpen");
    $(".kleft").removeClass("kursMobileInfo");

    $(".kright").attr("onclick","#");
    $(".kheader").attr("onclick","hideKursInfo('someInfo');");
}
// hide info
function hideKursInfo() {
    $(".kright").addClass("kursMobileOpen");
    $(".kleft").addClass("kursMobileInfo");

    $(".kright").attr("onclick","showKursInfo('someInfo');");
    $(".kheader").attr("onclick","#");
}

Thanks for your help!

And to the ones who are disliking the question: Please tell me why this is a bad question?

Don't use attr for register click events. Use click or on !

// show info
function showKursInfo() {
    $(".kright").removeClass("kursMobileOpen");
    $(".kleft").removeClass("kursMobileInfo");

    $(".kright").off("click");
    $(".kheader").on("click", hideKursInfo);
}

// hide info
function hideKursInfo() {
    $(".kright").addClass("kursMobileOpen");
    $(".kleft").addClass("kursMobileInfo");

    $(".kright").on("click", showKursInfo);
    $(".kheader").off("click");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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