JavaScript:如何在HTML textarea中添加换行符? [英] JavaScript: How to add line breaks to an HTML textarea?

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

问题描述

我正在使用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 \\\ ?)不一样的事实为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 />');

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

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