我正在寻找从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);
}
这是现在的输出:
如您所见,编码和解码无法正常工作,因为不会保留换行符。我需要改变什么?
答案 0 :(得分:4)
好吧,我想你需要用decodeURIComponent()
function PutTextIntoDiv() {
var TheText = encodeURIComponent($('#TheTextInput').val());
$('#TheOutput').text(decodeURIComponent(TheText));
}
并将<div>
替换为HTML中的<pre>
:
<pre id="TheOutput"></pre>
另一种选择,如果你想拥有明确的<br>
标签(或其他):
var brText = TheText.replace(/%0A/g,"<br/>");