需求:在textarea中輸入文字,提交給後臺後,後臺輸出在另外一個頁面,文字按原格式顯示。html
<textarea name="" id="test_new_line" cols="30" rows="10"></textarea>
<input type="button" id="submit" value="提交"/>
<div id="result"></div>chrome
js:spa
document.getElementById("submit").onclick = function(){
var newString = document.getElementById("test_new_line").value.replace(/\n/g, '_@').replace(/\r/g, '_#');
document.getElementById("result").innerHTML = newString;
};htm
輸入:blog
document.getElementById("submit").onclick = function(){
var newString = document.getElementById("test_new_line").value.replace(/\n/g, '_@').replace(/\r/g, '_#');get
newString = newString.replace(/_#_@/g, '<br/>');//IE7-8input
newString = newString.replace(/_@/g, '<br/>');//IE9、FF、chrome
newString = newString.replace(/\s/g, ' ');//空格處理
document.getElementById("result").innerHTML = newString;
};it