01前端/清理 Excel 导出的 HTML 的多余属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var whiteList = ["rowspan", "colspan"];

[...document.querySelectorAll("table")].forEach(table => {
rmAttr(table);
[...table.querySelectorAll("tr")].forEach(tr => {
rmAttr(tr);
[...tr.querySelectorAll("td")].forEach(td => {
rmAttr(td);
});
});
console.log(table.outerHTML);
});

function rmAttr(dom) {
[...dom.attributes].forEach(attr => {
if (!~whiteList.indexOf(attr.name)) {
dom.removeAttribute(attr.name);
}
});
}