Textarea中的新行将转换为< br/> [英] New Line in Textarea to be converted to <br/>

查看:119
本文介绍了Textarea中的新行将转换为< br/>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有很多关于转换br/>或保留跨不同语言的换行符的线程,但是关于文本区域的线程并不多.

There's a lot of threads here about converting br/> or preserving newlines across different languages, but not many regarding textarea.

我有这个脚本:

var boxText = "";
$("textarea.BoxText").live('dblclick', function () {
    boxText = $(this).val().replace(/ /g, "<br/>");
  $(this).replaceWith( '<div class="BoxText">' + $(this).val() + '</div>' );

});
$("div.BoxText").live('dblclick', function () {
  $(this).replaceWith( '<textarea form="HTML" class="BoxText">' + boxText + '</textarea>' );
});

我有一个textarea元素,可编辑.当用户双击它时,它将转换为div.但是,在div中,不保留换行符.我想只将新行转换为
,目前,所有空格都在转换中.我有第二个脚本将其转换回textarea,因此是用于存储字符串的变量.我还需要将
转换为新行.

I have a textarea element, editable. When the user double-clicks on it, it converts into a div. However, in a div, the newlines are not preserved. I would like to convert just the new lines into
, currently, all spaces are being converted. I have a second script that converts it back to textarea, hence the variable for storing the string. I would need the
's to be reconverted into new lines as well.

这似乎是多余的,但我对此有充分的理由.

This may seem redundant, but i have a good reason for this.

推荐答案

这会将换行符替换为HTML换行标记.不同的组合将涵盖不同的浏览器/系统以及如何解释换行符.

This will replace line breaks to HTML break tags. The different combinations are to cover the different browsers/systems and how line breaks are interpreted.


$(this).val().replace(/\r\n|\r|\n/g,"<br />")

这将使它重新出现-还介绍了不同浏览器如何解释innerHTML.

This will bring it back to new lines - also covering how different browsers interpret innerHTML.


boxText.replace(/<br\s?\/?>/g,"\n");

这篇关于Textarea中的新行将转换为&lt; br/&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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