简体   繁体   中英

Toggling classes in jquery when a condition is met

I have a bit of code written in JavaScript that changes the CSS class when a condition is met...

The problem with this (and many other bits of JavaScript for my site) is that i need to start writing them in JQuery instead.

The reason i am having to re write this one is because Internet Explorer is ignoring classList.add & classList.remove

I am starting to read up on JQuery but until then would one of you kind people be able to convert the following code to JQuery.

I wont show you how far i have got because its a mess :)

JAVASCRIPT CODE:

var animate = document.getElementsByClassName('nowPlayNoAnimate');

for (var i = 0; i < animate.length; i++) {
    if (animate[i].parentNode.offsetWidth < animate[i].offsetWidth) {
       animate[i].classList.add("nowPlayAnimate");                  
       }
    else animate[i].classList.remove("nowPlayAnimate");
    }

Thank you for your time!

toggleClass with a switch will do that :

$('.nowPlayNoAnimate').each(function() {
  $(this).toggleClass('nowPlayAnimate', $(this).parent().width() < $(this).width());
});

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