javascript Javascript用新行替换空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13628117/
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
Javascript replace whitespace with new line
提问by user1863470
Is it possible in javascript to replace all whitespaces in
是否可以在javascript中替换所有空格
<input name="product_description[2][name]>"
<input name="product_description[2][name]>"
and turn them into line breaks and show them in
并将它们变成换行符并显示在
<textarea name="product_description[2][meta_keyword]">
<textarea name="product_description[2][meta_keyword]">
I am trying this
我正在尝试这个
function getElements()
{
var text = document.getElementsByName('product_description[2][name]').value;
var out = document.getElementsByName('product_description[2][meta_keyword]').value;}
function makelinebreaks(){
text = text.split(" ").join("\n")
out = text;}
回答by Jim Blackler
I don't really understand your HTML fragments, but to perform that operation on a JavaScript string text
, one way is.
我不太了解您的 HTML 片段,但是要对 JavaScript 字符串执行该操作text
,一种方法是。
text.split(" ").join("\n")
回答by looper
Using jQuery:
使用jQuery:
$("#textarea").val($("#input").val().replace(/ /g, "\r\n"));