1.删除所有属性
javascript - Remove all attributes - Stack Overflow
javascript
function delAttribute(element) {
while (element.attributes.length > 0)
element.removeAttribute(element.attributes[0].name);
}
javascript
var table = document.getElementsByTagName('table');
for (var i=0; i<table.length; i++) {
delAttribute(table[i]);
}
var tbody = document.getElementsByTagName('tbody');
for (var i=0; i<tbody.length; i++) {
delAttribute(tbody[i]);
}
var tr = document.getElementsByTagName('tr');
for (var i=0; i<tr.length; i++) {
delAttribute(tr[i]);
}
var td = document.getElementsByTagName('td');
for (var i=0; i<td.length; i++) {
delAttribute(td[i]);
td[i].innerHTML = td[i].textContent; //仅保留td文字部分
}
var div = document.getElementsByTagName('div');
for (var i=0; i<div.length; i++) {
delAttribute(div[i]);
}
var span = document.getElementsByTagName('span');
for (var i=0; i<span.length; i++) {
delAttribute(span[i]);
}
var p = document.getElementsByTagName('p');
for (var i=0; i<p.length; i++) {
delAttribute(p[i]);
}
2.预览
javascript
var s = document.documentElement.outerHTML;
//换行
s = s.replace('<html>', '<html>\n').replace('<head>', '<head>\n').replace('<style>', '<style>\n').replace('</style>', '\n</style>').replace('</head>', '\n</head>').replace('<body>', '\n<body>').replace('<table>', '\n<table>\n').replace('<tbody>', '\n<tbody>\n').replaceAll('</tr>', '</tr>\n').replace('</tbody>', '</tbody>\n').replace('</table>', '</table>\n').replace('</body>', '</body>\n');
//中文乱码,https://blog.csdn.net/XUANEER/article/details/108469640
var blob = new Blob(['\ufeff', s], { 'type': 'text/html' });
var url = URL.createObjectURL(blob);
window.open(url);
3.保存
调试工具,网络,另存为