JS文本框的換行


<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>讓HTML文本框textarea里的換行等格式用js保留下來</title>

</head>

<body>

<form id="form1" name="form1" method="post" action="">

<textarea name="textContent" cols="100" rows="15" id="textContent"></textarea> <br />

<input type="button" name="Submit" value="轉換輸出" onclick="outHTML()" />

<input name="checkbox" type="checkbox" id="checkbox" value="checkbox" checked="checked" />

<label for="checkbox" style="color:#F00">啟用轉換輸出</label>

</form>

<div id="out" style="border:1px solid #F90; padding:10px; background:#FFF8DC; margin-top:10px">請在上面文本框錄入帶HTML標記的文本測試!</div>

<script type="text/javascript">

function outHTML() {

var getValue=document.getElementById("textContent").value;

var endValue=((getValue.replace(/<(.+?)>/gi,"&lt;$1&gt;")).replace(//gi,"&nbsp;")).replace(/\n/gi,"|");

/* (1)轉義“<”、“>” (2)改半角空格為&nbsp; (3)保留換行 */

if (document.getElementById("checkbox").checked==true) {

document.getElementById("out").innerHTML=endValue;

}else{

document.getElementById("out").innerHTML=getValue;

}

}

</script>

</body>

</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM