2

我希望从 textare 创建一个电子邮件模板,该模板可以显示在 div 中,也可以通过 mailto 发送。这是一个jsFiddle

HTML 非常简单:

<textarea id="TheTextInput" rows="5"></textarea>
<input type="button" id="TheButton" value="click" />
<div id="TheOutput"></div>

我目前正在尝试的 javascript 看起来像这样:

$(document).ready(function () {
    $('#TheButton').click(PutTextIntoDiv);
});

function PutTextIntoDiv() {
    var TheText = encodeURIComponent($('#TheTextInput').val());
    $('#TheOutput').text(TheText);
}

这是现在的输出:

在此处输入图像描述

如您所见,编码和解码不起作用,因为没有保留换行符。我需要改变什么?

4

1 回答 1

4

好吧,我想你需要用decodeURIComponent()

function PutTextIntoDiv() {
    var TheText = encodeURIComponent($('#TheTextInput').val());
    $('#TheOutput').text(decodeURIComponent(TheText));
}

并在您的 HTML 中替换<div>为:<pre>

<pre id="TheOutput"></pre>

如果您想拥有明确的<br>标签(或其他),另一种选择:

var brText = TheText.replace(/%0A/g,"<br/>");
于 2013-05-09T01:21:16.283 回答