如何在HTML文本区域添加换行符? [英] How to add line breaks to an HTML textarea?

查看:560
本文介绍了如何在HTML文本区域添加换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JavaScript编辑<textarea>.问题是当我在其中换行时,它们不会显示.我该怎么办?

I’m editing a <textarea> with JavaScript. The problem is that when I make line breaks in it, they won’t display. How can I do this?

我获得了编写函数的价值,但不会给出换行符.

I’m getting the value to write a function, but it won’t give line breaks.

推荐答案

问题来自以下事实:换行符(\n\r?)与HTML <br/>标签不同

Problem comes from the fact that line breaks (\n\r?) are not the same as HTML <br/> tags

var text = document.forms[0].txt.value;
text = text.replace(/\r?\n/g, '<br />');

这篇关于如何在HTML文本区域添加换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆