textarea内部换行实现

时间:2021-06-22 13:06:50

当在使用textarea的时候,有一次需求,需要做到自定义换行,而不是通过textarea定宽来自动换行,其实在html中可以直接通过<br/>来换行,同时也想到用\n来实现换行,其结果是根本不鸟你。

<textarea cols="50" rows="5" id="textareaid">"adfsadfs<br/>fddsadfds \n dfadadf"</textarea>

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbYAAABiCAIAAACzs2GqAAAFZ0lEQVR4nO3azYrTahzA4d5RryhXIQ6KguCuGwVduJNxJczhQGBAxEEUdeW6u7NQHDjqpOl0PkB3nkWcNmnzb5tOMiOnz0MQ56NvkrfJr0k7vdu3b/8CoE5PIgEiEgkQkkiAUH0iP33+cvD63V9/71ssFsv/ezl4/e7T5y/NEvnl8Oj7aNJmigGuxM+fPw8PD79+/Zrn+XHJZDIp/p06OTk5PT09Ojr659O/0WhhIrvcBYCuSCRASCIBQhL55xoO+kl63RvxZzElXLUrT+Rw0O/1+oPhcNDv9XpNjvc0WXhAmvQu9AfD9Ye6ovFrRmz06LXXuXQ7o6243NYtHWej7Vl3bZd/plduz0bPe9P9Wm+9m58vtON6EpmkxeHQ9GhPk8oxMvfl5bUz/nDQn+7ZxptYvV4qD7lo5UqiX2hrApvO22XW28WFZFvHVdMHrrHeS50vtCBKZJ7nWZbled76jXaaFM/0cNDvzUWg9oV7+sraHwxnx1Dp13u9ysvr7Cf9JJmdTV2PX/5h6ZtpkqSzR8x+UKx1enWwcPDXXCzVXqXE21m/X/H3u563hutdYnFuiqkZXIy0Vkkabn/DeWh13urOF65KbSJHo1GWZXfu3ClXsq1EDgf9i6e8fOwM0/Tii9IpUP6lxUTUveqWvpcm5UO84/GDhFW+ObfLxclS/DBNyw8Mr5Tqb7cWtzPar3h/u5235utdpm56Sg9e4zqz+fPedB7aOt6i84WrspjIoo83bty4d+/eo0ePppXs+OOaysvo7I2YpHxUzB/7tYdU8D5Sp+OnSXTtMnfFU1lh+LbainO89lJ17jfq92vZ/nY5bxusd6maCapeKK8YaYPnvek8tHa8cc3mEln08ebNm4PB4NmzZ3t7e8+fPy8q2WkiKwfL7EC65CGVJrUXFh2M//urus8mmidy6Rled7/VSiIr47U8b43Xu9LCHHWbyKbz0M3xxnWYS2SWZQ8fPtzd3d3f3z84OHj79u2HDx/evHmTZVmniazei/TKX1SvN1YdUpWzY3bKdT1+aYRqJ6tvslfP3fozOb6EDD/UXHFKR/eg5f3teN6arne1ZXlZ/al30+1vOg+tHW9cu9qryEVdX0WW70uKt90rf/RwcbsymL59U7o9W7zlrblx63r8klknf7/bNHtM/TCVgcJuRnUMBgr2a6397WDemq53DaWZuhglSav/X6Lh9jedh9bmjevmT8f/KP40ugGTxRWQSICQRAKEJBIgJJEAIYkECEkkQEgiAUISCRCSSICQRAKEJBIgJJEAIYkECEkkQEgiAUISCRCSSICQRAKEJBIgJJEAIYkECEkkQEgiAUISCRCSSICQRAKEJBIgJJEAIYkECEkkQEgiAUISCRCSSICQRAKEJBIgJJEAIYkECEkkQEgiAUISCRCSSICQRAKEJBIgJJEAIYkECEkkQEgiAUISCRCSSICQRAKEJBIgJJEAIYkECEkkQEgiAUISCRCaS+RoNMrq5HkukcDWmUtklmV3797d2dnZ2dm5devW/fv3nzx5sre3l2WZRAJbp/Yq8sGDB48fP97d3X3x4sX79+9dRQJbavG9yKKST58+ffny5cePH4s+ei8S2EZFIr99+zYej6efzOR5nmXZq1evpn2USGAbFYnMsuz4+PikZDwej0aj8Xg8/c7p6enZ2ZlEAlukSGSRwtOlzs7OJBLYLkUiJ5PJYhCnWSyTSGCLFIlcrOH5+Xnx7xyJBLZIkcjFFEY2TOT30aTLvQDoRJHIoyYaJ/L8/Mfh92OLxWLZhuX8/EezRALwSyIBlpBIgJBEAoQkEiAkkQAhiQQISSRASCIBQhIJEJJIgJBEAoT+A53T8W/PNPOxAAAAAElFTkSuQmCC" alt="" />

百度几翻网上确实有说可以用\r\n去实现换行,如果只兼容微软系统的话直接\n即可,但是实验确实不行,后面想到是不是必须通过JS赋值才可以实现,于是出现如下代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<textarea cols="50" rows="5" id="textareaid">"adfsadfs<br/>fddsadfds \n dfadadf"</textarea>
<script type="text/javascript">
document.onclick=function(){
alert(1);
var str='5456455d4fgdfgdgdg \n dsfafdadfadfafa';
document.getElementById("textareaid").value=str;
}
</script> </body>
</html>

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbcAAABsCAIAAABme2vkAAAFiklEQVR4nO3dT2sTeRjA8byjvpw95VWIVVAQvOWioAvepJ7EngKCK3poQZcFPZpF2OMKVjuZtE099NY9jKaTZObJTJxps87nwyCY/PpMPOTL5E9/9s4BKNe76gcAsNFUEiCikgARlQSIqCRARCUBIr3z8/P9jwc3nr3/7fc9h8Ph+LWPG8/e7388qF3JP/9JP3yethJhgDadnZ19+vTp4OAgTdOjnOPj4+zPmZOTk+l0enh4+Mffh7VO8b2S7Tx+gHapJEBEJQEiKgkQuZJKDvu9mf5wcf2w3+ttDUZV148GW9k98z9TZ078eH4sWLxnef7CrOU7yxTMb2pOo+uhi66okuXPzGF/azAc9BfqVrZ+2C97lteZEz2esjVF8yuOqjK/qTmNr4fOKatkmqZJkqRpeqmVHA22tgaj81HlupVcZtWeU3b7xdXc3JrC+bOL2qWr0tk9/eHc1Vvx/KbmNLceOq2wkuPxOEmSW7du5UPZzivufOaG/e/Py+W6Fa4fDfqDwY+7cs/ounOKb/9ewosFszOUzj8vDO5seVahH0PL5zc0p6H10HXLlcwSee3atTt37jx48GAWynY+vbl4as69O1h6IZNbP/ccHw221p5TePtCAavOX67b3AXvRaLK5zc0p6H10HkLlcwSef369cFg8OTJk93d3adPn2ahbOkz7qJXuwXXaEXrh/25S7/ln6k4p/D2FRUrm7+ibhd/+7lKVpjT0HrovIVKJkly//79nZ2d58+fv379en9//+3bt3t7e0mSNFbJfNyKr1nmn8nB+txzeXVty+aU3D7XreJPiaq+4s6P71Wb38CchtZD1xVeSy5r+Fry4kVr+Rd7ir+qU/R1nOU3GuvOKb499zHKVvYG6PwHOAs/sfBKPPeAcuP7/fzlb+H8puY0th66zbfKL1XhC/dLmNP2eviFqeQlyF28/VR76s5pez10gkoCRFQSIKKSABGVBIioJEBkMytZY6+FujuMhesb2HnsqnZOW+u8wGqbWcnzyjt6Nb5R2E8OvKqd02yABm3ZqEqu3NGrt9Xvz77uXLrD2NzvllTZ2azkvCVzau941vKc+ucFaticSkY7lc39FuDKa67RcDia3b3Qh+X15ectnlN7x7OW56y3HqhqYyoZ7ZETvOdWvMlF7uJqVU2C8xbOqb2XT8tz1lwPVPW/qGTO4r6QhdeG+dfq/XUrWTKndt1anrPmeqCqjalkpR29qlQy3gBsRU1y5y2bU3fHs7bnrLceqGpzKlltJ7HCfcTm78nv2TDI/d8EpTuSlZy3bE7dHc/anlP7vEAdm1RJgM2jkgARlQSIqCRARCUBIioJEFFJgIhKAkRUEiCikgARlQSIqCRARCUBIioJEFFJgIhKAkRUEiCikgARlQSIqCRARCUBIioJEFFJgIhKAkRUEiCikgARlQSIqCRARCUBIioJEFFJgIhKAkRUEiCikgARlQSIqCRARCUBIioJEFFJgIhKAkRUEiCikgCRhUqOx+OkSJqmKgl00UIlkyS5ffv29vb29vb2zZs37969++jRo93d3SRJVBLoosJryXv37j18+HBnZ+fFixdv3rxxLQl01/L7klkoHz9+/PLly3fv3mWJ9L4k0FFZJb98+TKZTGYf1KRpmiTJq1evZolUSaCjskomSXJ0dHSSM5lMxuPxZDKZ3TKdTk9PT1US6JasklkNp6HT01OVBDonq+Tx8fFyE2dlzFNJoFuySi4H8du3b9mfC1QS6Jaskss1LLN+JT98nrbzTwBoUVbJwzrWqeTXk7O//j1xOByOLhxfT85qVxKAMioJEFFJgIhKAkRUEiCikgARlQSIqCRARCUBIioJEFFJgIhKAkRUEiCikgARlQSIqCRARCUBIioJEFFJgMh/nEhyOzKfPoUAAAAASUVORK5CYII=" alt="" />

成功的实现了textarea换行,所以我推断,要想实现textarea换行,必须得动态JS赋值才行。

楼下有大牛@ wangmeijian 给出解决方法,如果不想动态JS赋值就实现换行:可以通过输入HTML实体换行符“ ”来实现换行,确实可行,nice!

测试地址

注:百度上有说明要想实现兼容linux,unix,Mac OS,window,可以把\n换成\r\n,但我在苹果系统下用\n也实现了换行,linux,unix系统下没测试,求解

个人知识有限,如有错误的地方还望指正,共同学习共同进步!