本帖最后由 claymore1114 于 2013-12-19 11:16:57 编辑

解决方案 »

  1.   

    String.prototype.trim = function(){
    return this.replace(/(^[\s\n\r]*)|([\s\n\r]*$)/g, '');
    }
      

  2.   


        var s = '<span style="color: #000000;">   1     </span><span style="color: #0000FF;">2 <b style="color: #0000FF;"> 3</b></span>';
        s = s.replace(/>([^<]+)/g, function ($0, $1) { return '>' + $1.replace(/ /g, '&nbsp;'); });
        alert(s)
      

  3.   

    String.prototype.trim1 = function(){
    return this.split(" ").join("");
    }
      

  4.   

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <style type="text/css">
    </style>
    </head>
    <body>
    <div id ="aa">
    <span style="color: #000000;">   1     </span>
    <span style="color: #0000FF;">2 <b style="color: #0000FF;"> 3 <i>sd  </i></b></span>
    </div>
    <script>
    var ele = document.getElementById("aa"),
        htm = ele.innerHTML;
    console.log(htm);
        htm = htm.replace(/\<span(?:\s+[^\>]+|)\>(.+)\<\/span\>/g,function($1,$2){
            return $1.replace($2,$2.replace(/(?:^|\>)([^\<]+)(?:\<|$)/g,function($1,$2){
    return $1.replace($2,$2.replace(/^\s+|\s+$/g,"&nbsp;"));
    }));
        });
        ele.innerHTML = htm;
    console.log(htm);
    </script>
    </body>
    </html>试试这个行不。