首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >过滤所有属性和属性值

过滤所有属性和属性值
EN

Stack Overflow用户
提问于 2017-09-11 15:08:50
回答 1查看 270关注 0票数 0

使用纯Javascript,如何过滤目标元素中所有属性和属性的值?

下面是我为实现目标而尝试实现的示例代码。但是,我不知道如何获取所有属性名,然后获取属性名的值来筛选它。如果没有列出属性名,我希望删除该属性。如果允许的话,我只想保留允许的价值。

代码语言:javascript
复制
(function() {
  //array allowed attr and allowed value of attr
  /*
  var allowedAttrValue = {};

  allowedAttrValue['class'] = 'dont_remove'; // I want to use dont_remove class

  allowedAttrValue['class'] = 'i_want_this_class'; // I want to use i_want_this_class class

  allowedAttrValue['data-dont-remove='] = 'whatever'; //what ever value in this attribute are allowed.

  // 1. I need to remove all attribute not listed 
  // 2. I want to filter value of allowed attribute. if value not listed I want to keep only allowed attribute (especially class; have multiple value separatedby space).
  */

  var
    editor = document.getElementById("post-editor"),
    editorParagraph = editor.querySelectorAll("*");

  for (i = 0; i < editorParagraph.length; ++i) {
    elem = editorParagraph[i];
    if (elem.attributes) {
      //console.log(elem.attributes); //return namedNodeMap object. how to get attribute name and attribute value?
      var ii;
      for (ii = 0; ii < elem.attributes.length; ++ii) {
        var elemAttr = elem.attributes[ii];
        console.log(elemAttr); //retrun directly data-attr="haha" data-wtf="remove-me" class="hehe" and all.
      }
    }
  }

})(); //end
代码语言:javascript
复制
<div id="post-editor" class="preview">
  <div data-attr="haha" data-wtf="remove me">
    bla bla
  </div>
  <div class="hehe dont_remove" data-something="haha">
    bla bla
  </div>
  <div data-dont-remove="value" data-randomly="haha">
    bla bla
  </div>
  <div class="this_class_not_listed i_want_this_class" data-remove-all="haha">
    bla bla
  </div>
  <div data-data="haha">
    bla bla
  </div>
</div>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-11 15:29:42

下面是一个使用attributesremoveNamedItem的简单示例

单击remove Me时,它将从第一个div中移除data-remove-me属性,因为它不在keepList中。然后你应该看到红色的div变成白色..。

代码语言:javascript
复制
var 
  keepList = {
    'data-keep-me': true
  };

document.querySelector('button').onclick = function () {
  var divs = document.querySelectorAll('div');
  for (var l = 0; l < divs.length; l ++) {
    var d = divs[l];
    for (var ll = d.attributes.length -1; ll >= 0; ll--) {
      var a = d.attributes[ll];
      if (!keepList[a.name]) 
        d.attributes.removeNamedItem(a.name);
    }
  }
}
代码语言:javascript
复制
[data-remove-me] {
  background-color: red;
}
[data-keep-me] {
  background-color: yellow;
}
代码语言:javascript
复制
<div data-remove-me="Remove Me">
Remove Me
</div>

<div data-keep-me="Keep Me">
Keep Me
</div>

<button>Remove Them</button>

这里也是你的例子,这些mods。但是,您需要使用对象检查来查看更改。

注意,我还按反向顺序遍历了这些属性,这是因为当您删除它们时,顺序/长度会发生变化。

代码语言:javascript
复制
(function() {
  //array allowed attr and allowed value of attr  
  var allowedAttrValue = {
    class: true,
    'data-dont-remove': true
  };

  var
    editor = document.getElementById("post-editor"),
    editorParagraph = editor.querySelectorAll("*");

  for (i = 0; i < editorParagraph.length; ++i) {
    elem = editorParagraph[i];
    if (elem.attributes) {
      var ii;
      for (ii = elem.attributes.length -1; ii >=0; --ii) {
        var elemAttr = elem.attributes[ii];
        if (!allowedAttrValue[elemAttr.name]) 
          elem.attributes.removeNamedItem(elemAttr.name);
      }
    }
  }

})(); //end
代码语言:javascript
复制
<div id="post-editor" class="preview">
  <div data-attr="haha" data-wtf="remove me" data-xyz="">
    bla bla
  </div>
  <div class="hehe dont_remove" data-something="haha">
    bla bla
  </div>
  <div data-dont-remove="value" data-randomly="haha">
    bla bla
  </div>
  <div class="this_class_not_listed i_want_this_class" data-remove-all="haha">
    bla bla
  </div>
  <div data-data="haha">
    bla bla
  </div>
</div>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46158923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档