javascript 在 textarea 中按 Enter 时段落中的新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14176849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
New line in paragraph when Enter is pressed in textarea
提问by Csak Zoli
document.getElementById('area').onkeyup=keydown;
function keydown (event) {
if (event.target.id=='area' && event.which==13) {
document.getElementById('arsh').innerHTML+="\n";
}
}
Arsh is the ID of my paragraph where i want to add the new line why is this not working can anyone help me ?I just want to add a new line when Enter is pressed in the textarea (ID area) to my Paragraph (ID arsh).
Arsh 是我想添加新行的段落的 ID 为什么这不起作用任何人都可以帮助我吗?我只想在文本区域(ID 区域)中按 Enter 时添加一个新行到我的段落(ID )。
here is the HTML
这是 HTML
<textarea ID="area" class="in">
Message
</textarea>
<br />
<br />
<p ID="tiesh" class="sh">
</p>
<br />
<br />
<p ID="arsh" class="sh">
</p>
<br />
</div>
I am basically trying to do it like here in stackoverflow when you press enter in the textarea down the paragraph jumps down a line also.
我基本上是在stackoverflow中尝试这样做,当您在textarea中按下回车键时,段落也会跳下一行。
回答by Austin Brunkhorst
Using CSS, give #arsh
(or any other elements that you want to render new lines) this attribute.
使用 CSS,给#arsh
(或任何其他要呈现新行的元素)这个属性。
white-space: pre-wrap;
回答by cbp
The newline character (\n) do not render as new lines on the screen. You probably need to add an HTML break instead:
换行符 (\n) 不会在屏幕上呈现为新行。您可能需要添加一个 HTML 中断:
document.getElementById('arsh').innerHTML+="<br />";
回答by user3510753
document.getElementById('arsh').innerHTML=document.getElementById("area").value.replace(/\n/g,'<br />')
this will do the job
这将完成工作
回答by M?m?n ??m?s??r?
<p style="white-space: pre-wrap;"><?php echo $row['description_text'];?></p>