方法一:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <textarea name="" id="txt" cols="30" rows="10"></textarea> <input type="button" id="btn" value="点击按钮"> <div id="desc"></div> </body> <script> var txt = document.getElementById('txt') var btn = document.getElementById('btn') var desc = document.getElementById('desc') btn.onclick = function () { var val = txt.value val = val.replace(/\r\n/g, '<br/>'); //IE9、FF、chrome val = val.replace(/\n/g, '<br/>'); //IE7-8 val = val.replace(/\s/g, ' '); //空格处理 desc.innerHTML = val; } </script> </html>
方法二:
步骤:1. 用<pre><c:out value="<%=从数据库显示的字段%>"></c:out></pre> 2. 显示页面中增加: <style type="text/css">
pre {
white-space: pre-wrap;
white-space: -moz-pre-wrap;
white-space: -pre-wrap;
white-space: -o-pre-wrap;
word-wrap: break-word;
}
</style>
原文链接:https://blog.csdn.net/moneyzhong/article/details/8820324 !!!!第二种方法很方便