有時候textarea中用戶填入的文本需要保留格式進行打印或者其他的操作,所以這裏介紹一個小方法來實現:
var text = $("#textareaId").html();
if(rext!=""){
var textareaContent = text.replace(/\n/g,"</br>");// text.replace(/要替換的內容就算是字符串也不用引號/g,替換爲的內容),/g表示替換全部,
//與java中的replaceAll相似,
textareaContent =textareaContent .replace(/ /g," ");
$("#anotherTextarea").html(textareaContent )
}