假设我有以下HTML代码
<table width="600" border="0" cellpadding="0" cellspacing="0" align="center" bgcolor="#ffffff">
<tr>
<td><img src="/images/pho_01.jpg" width="600" height="63" border="none" style="border: 0px;" alt="photo" /></td>
<td></td>
<td></td>
</tr>
</table>
HTML代码不可读,我想转换为喜欢的内容
<table width="600" border="0" cellpadding="0" cellspacing="0" align="center" bgcolor="#ffffff">
<tr>
<td><img src="/images/pho_01.jpg" width="600" height="63" border="none" style="border: 0px;" alt="photo" /></td>
<td></td>
<td></td>
</tr>
</table>
我曾经使用$.trim(),但没有用,有人可以建议一种方法
1.删除空格(但不要删除属性之间的空格,如table width="600"
...)
2.删除空行
由于
答案 0 :(得分:4)
假设您的html存储在javascript字符串中,您必须将其拆分为多行并在每行上使用trim。
e.g。
function multilineTrim(htmlString) {
// split the string into an array by line separator
// call $.trim on each line
// filter out the empty lines
// join the array of lines back into a string
return htmlString.split("\n").map($.trim).filter(function(line) { return line != "" }).join("\n");
}
答案 1 :(得分:1)
尝试http://jsbeautifier.org/ 它也适用于HTML
答案 2 :(得分:0)