解析DOM,删除x的所有属性和y的所有类



我使用typescript来调整模板化的html构建文件。在我的TS文件中,我有以下代码:

let body = document.querySelector("body");
// Looking at using this to remove all attributes of certain types/names
[...elem.attributes].forEach(attr => elem.removeAttribute(attr.name))

body变量是来自DOM的BodyElement,可以很好地把它拉进来。我需要解析所有元素以删除名为:

的属性
click.delegate="doSomething();"
show.bind="toggle();"

和类:

aurelia-hide

这将是伟大的forEach通过这一次在TS快速摆脱它。

看一下这个代码片段:

const removeClassElement = document.getElementById('1');
removeClassElement.className = "";
// or classList = "" or classList = null or classList.remove(...removeClassElement.classList);
console.log(removeClassElement);
const removeAttributes = document.getElementById('2');
[...removeAttributes.attributes].forEach(attr => removeAttributes.removeAttribute(attr.name));
console.log(removeAttributes);
<p id="1" class="one two three">Lorem ipsum dolor sit amet</p>
<p id="2" style="color: green; font-weight: bold" title="title">Lorem ipsum dolor sit amet</p>

最新更新