简体   繁体   English

如何删除窗口宽度的类列表-Jquery / Javascript

[英]How To Remove A List Of Classes At Window Width - Jquery/Javascript

Can someone help me optimize this Jquery to remove a list of classes from the DOM? 有人可以帮助我优化此Jquery来从DOM中删除类列表吗? The code that I have works, but there must be a way to do this without having to list each class individually. 我拥有的代码可以运行,但是必须有一种方法可以不必单独列出每个类。

jQuery(document).ready(function($){
   if(window.innerWidth <= 960) {
      $('.wow').removeClass("wow");
      $('.animated').removeClass("animated");
      $('.bounceInDown').removeClass( 'bounceInDown' );
      $('.slideInLeft').removeClass( 'slideInLeft' );
      $('.slideInRight').removeClass( 'slideInRight' );
      $('.zoomInUp').removeClass( 'zoomInUp' );
   }
});

You could list all classes into a single selector and remove all classes at once, like $('.animated,.bounceInDown,.slideInLeft,.slideInRight,.zoomInUp').removeClass('animated,bounceInDown,slideInLeft,slideInRight,zoomInUp'); 您可以将所有类列出到单个选择器中,然后一次删除所有类,例如$('.animated,.bounceInDown,.slideInLeft,.slideInRight,.zoomInUp').removeClass('animated,bounceInDown,slideInLeft,slideInRight,zoomInUp'); , but this will remove all of listed classes for each of the selected elements, which might not be what you want to accomplish. ,但是这将删除所有选定元素的所有列出的类,这可能不是您想要完成的。

You could use Array.prototype.forEach like: 您可以像这样使用Array.prototype.forEach

["animated", "bounceInDown", "slideInLeft", "slideInRight", "zoomInUp"].forEach(function (e) {
  $('.' + e).removeClass(e);
});

You could try extending jQuery: 您可以尝试扩展jQuery:

$.fn.classGoByeBye = function(){
    var selector = this.selector.replace('.', '');
    $(this).each(function(){
        this.classList.remove(selector);
    });
};

//usage:
$('.wow').classGoByeBye();

One possible solution would be to place your list of classes in an array and then iterate over them within your condition. 一种可能的解决方案是将您的类列表放置在数组中,然后在您的条件下对其进行迭代。 For example... 例如...

if (window.innerWidth <= 960) {
    $.each(['wow', 'animated'], function(index, value) {
        $("." + value).removeClass("." + value);
    });
}

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

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