<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, "");没用)就去不掉。
不知道怎样才能去掉。
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>这个应该可以。
谢谢给分。
var html = document.getElementById("div1").innerHTML.replace(/^\s*|\s*$/g, "")
alert(html);
alert(html);
var html = document.getElementById("div1").innerHTML.replace(/\s+/g, "") //全部空格
alert(html);