简体   繁体   English

从某些格式的所有div元素中删除特定的类

[英]Removing specific classes from all div elements of certain format

I would like to remove all classes starting with nf- 我想删除所有以nf-开头的类

Console image 控制台映像

JSfiddle https://jsfiddle.net/zapjelly/fda3Lm84/11/ JSfiddle https://jsfiddle.net/zapjelly/fda3Lm84/11/

I have the following HTML/JS: 我有以下HTML / JS:

 var nfClasses = document.querySelectorAll('.custom-nf [class*="nf-"]'); Array.prototype.map.call(nfClasses, function(elem) { Array.prototype.map.call(elem.classList, function(classStr) { if (classStr.match(/^nf\\-/)) elem.classList.remove(classStr); }); }); 
 <p>Remember to inspect element on the input below</p> <div class="custom-nf"> <div class="input nf-input-outer nf-validation"> <div class="nf-container"> <div class="nf-outer nf-outer-input nf-outer-validation"> <div class="nf-middle"> <div class="nf-inner"> <label for="dummy" class="nf-label"></label> <input id="dummy" type="text" class="nf-input"> </div> </div> </div> </div> </div> </div> 

As stated by MDN , the .classList property returns a live DOMTokenList of the class attributes of the element. MDN所述, .classList属性返回元素的类属性的实时 DOMTokenList The key point is that the list is live , which means that as you remove classes, you are inadvertently skipping over some of the other classes while iterating over them. 关键是列表是实时的 ,这意味着在删除类时,您在遍历其他类时会无意间跳过了一些其他类。

Based on the code that you provided, you could simply create a copy of the class list so that it is no longer live. 根据您提供的代码,您可以简单地创建类列表的副本,以使其不再存在。 You can do this by calling the .slice() method: 您可以通过调用.slice()方法来执行此操作:

Updated Example 更新示例

var nfClasses = document.querySelectorAll('.custom-nf [class*="nf-"]');
Array.prototype.forEach.call(nfClasses, function(element) {
  var classListCopy = Array.prototype.slice.call(element.classList);
  classListCopy.forEach(function(className) {
    if (className.match(/^nf\-/)) {
      element.classList.remove(className);
    }
  });
});

Alternatively, you could also iterate over the classes backwards. 另外,您也可以向后遍历这些类。 In doing so, none of the classes will be skipped: 这样做不会跳过所有类:

Updated Example 更新示例

var nfClasses = document.querySelectorAll('.custom-nf [class*="nf-"]');
Array.prototype.forEach.call(nfClasses, function(element) {
  for (var i = element.classList.length; i >= 0; i--) {
    if (element.classList[i] && element.classList[i].match(/^nf\-/)) {
      element.classList.remove(element.classList[i]);
    }
  }
});

A third option would be to just use a regular expression to replace the classes: 第三种选择是仅使用正则表达式替换类:

var nfClasses = document.querySelectorAll('.custom-nf [class*="nf-"]');
Array.prototype.forEach.call(nfClasses, function(element) {
  element.className = element.className.replace(/\bnf-\S+\b/g, '').trim();
});

Updated inner loop to slice as suggested and working now 更新了内部循环以按照建议进行切片并立即工作

https://jsfiddle.net/zapjelly/fda3Lm84/12/ https://jsfiddle.net/zapjelly/fda3Lm84/12/

var nfClasses = document.querySelectorAll('.custom-nf [class*="nf-"]');

Array.prototype.map.call(nfClasses,
    function(elem){
    Array.prototype.slice.call(elem.classList).map(
        function(classStr){
            if(classStr.match(/^nf\-/)) elem.classList.remove(classStr);
        })
  });

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

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