简体   繁体   English

jQuery删除包含特定单词的类

[英]jquery remove class which contains a specific word

I have many classes in my HTML for example .margin-15 , .margin-30 etc. 我的HTML中有很多类,例如.margin-15.margin-30等。

My question is how can I delete all classes containing margin- . 我的问题是如何删除所有包含margin-类。

I tried with .removeClass but I do not know how to delete all classes that contain a particular string. 我尝试使用.removeClass,但是我不知道如何删除包含特定字符串的所有类。

Thanks for help. 感谢帮助。

An alternative is selecting the elements with classnames which contain margin- and then loop and filter those who don't contain margin- 一种替代方法是选择类名包含margin-的元素,然后循环并过滤不包含margin-的元素。

 $( "[class*='margin-']" ).each(function(_, ele) { var currentClass = $(ele).attr('class'); console.log("Current class:", currentClass); var classNames = currentClass.split(/\\s+/).filter(function(c) { return !c.includes('margin-'); }).join(' '); console.log("After filter:", classNames); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="margin-12 eleclass"></div> 

Here is an example of how you can do it : in my example I want to remove all the class that contains color- : 这是一个如何实现的示例:在我的示例中,我想删除所有包含color-的类:

1/ You select all element that contains color- (and maybe other class) 1 /您选择所有包含color-元素(可能还有其他类别)

2/ I loop throught each element and get all the class 2 /我遍历每个元素并获得所有类

3/ I split the class by space ( " " ) so I now have an array with all the class for each element 3 /我将类按空格( " " )分割,因此现在有了一个数组,其中每个元素的所有类

4/ I loop throught my class array and check if the class contains color- 4 /我遍历我的类数组,并检查该类是否包含color-

5/ If I find the class that contains color- (here it can be color-red , color-blue , color-green ) I remove it from my block. 5 /如果我发现包含color-的类(这里可以是color-redcolor-bluecolor-green ),则将其从块中删除。

I think you can adapt it to remove all your margin- ! 我认为您可以对其进行调整以消除所有的margin-

 $(document).ready(function() { $("#remove").click(function() { var $div_list = $("div[class*=color-]"); $.each($div_list, function() { var $current_div = $(this); var class_list = $(this).attr("class"); class_list = class_list.split(/ +/); $.each(class_list, function(key, value) { var current_class = value; if (current_class.indexOf("color-") !== -1) { $current_div.removeClass(current_class); } }); }); }); }); 
 .color-red { color: red;} .color-blue { color : blue; } .color-green { color : green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="color-red test"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> </div> <div class="color-blue test2"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> </div> <div class="color-green test3 test4"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> </div> <button type="button" id="remove">Remove color</button> 

Is it what you are looking for? 是您要找的东西吗?

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

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