开发者

jQuery/JavaScript trim HTML that contains white space/empty line

开发者 https://www.devze.com 2023-03-23 19:17 出处:网络
Suppose I have the HTML code as below <table width=\"600\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\" bgcolor=\"#ffffff\">

Suppose I have the HTML code as below

<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>

The HTML code is not readable, I want to convert to something likes

<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>

I have ever used the $.trim(), but not works, could someone please suggest a way to

1. remove space(but don't remove the space between attribute likes tab开发者_如何学Pythonle width="600"...)

2. remove empty line

Thanks


Assuming your html is stored in a javascript string, you'll have to split it up into multiple lines and use trim on each line.

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");
}


try http://jsbeautifier.org/ It works with HTML too


Try this http://www.textfixer.com/tools/remove-line-breaks.php

0

精彩评论

暂无评论...
验证码 换一张
取 消