<div id="div1">
     hello world!
     <a href="http://www.baidu.com"> 连接到百度</a>
             <a href="http://www.baidu.com"> 连接到百度</a>
<div>
alert(document.getElementById("div1").innerHTML);在ie下会自动给hello world前面的空格去掉,在firefox下是会是\n+空格 
在用正则表达式(var rgexp = new RegExp("(\n*\s*\r*)", "gi");var inputHtml = obj.innerHTML.toString().replace(rgexp, "");没用)就去不掉。
不知道怎样才能去掉。

解决方案 »

  1.   

    是不一样的,楼主,在ie和ff的margin是不一样的,margin-left也是不一样的,默认的同样是不一样的,楼主可以详细研究一下,你写的是在默认的情况下执行的
      

  2.   

    String.prototype.Trim = function() { return this.replace(/(^\s*)|(\s*$)/g, ""); }
        String.prototype.LTrim = function() { return this.replace(/(^\s*)/g, ""); }
        String.prototype.RTrim = function() { return this.replace(/(\s*$)/g, ""); }
    function demo(){
        var rgexp = new RegExp("(\n*\s*\r*)", "");
        var inputHtml = document.getElementById("div1").innerHTML.toString().replace(rgexp, "").Trim();
        alert(inputHtml);
        alert(document.getElementById("div1").innerHTML);}
    </script>这个应该可以。
       谢谢给分。
      

  3.   


    var html = document.getElementById("div1").innerHTML.replace(/^\s*|\s*$/g, "")
    alert(html);
      

  4.   

    只去掉前面的用这个就行var html = document.getElementById("div1").innerHTML.replace(/^\s*/g, "")
    alert(html);
      

  5.   

    要全部去掉?var html = document.getElementById("div1").innerHTML.replace(/\b\s+/g, "") //行开头空格
    var html = document.getElementById("div1").innerHTML.replace(/\s+/g, "")   //全部空格
    alert(html);