不确定为什么我的textarea没有拿起换行符。我正在尝试使用文本模板预填充textarea,但我需要它来保留换行符而不是单行。有谁知道我错过了什么?
以下是https://jsfiddle.net/t8y1okpp/
的示例这是我的HTML,很简单:
<textarea class="form-control" rows="5" id="Revue" style="width: 100%; min-height: 200px; white-space: pre;"></textarea>
这是我的JavaScript:
var revue_text_template ="Multi-Client impact? \
1: \
2: \
3? \
4? \ 5?“; var revue_text_template_filtered = revue_text_template.replace(/ \ r \ n | \ r | \ n / g,“\ n”);
$('#Revue').val(revue_text_template_filtered);
答案 0 :(得分:2)
您应该使用\n
进行换行。
var revue_text_template = "Multi-Client impact? \nPrime Speaker(s): \n Component affected (infrastructure, application, server, network, etc.): \n Root Cause identified? If yes, what is the cause? \n How was the incident detected (alarm, client, vendor)? \n Was the incident caused by a planned change? If yes, what is the change number? \n Was recovery optimal? If not, why? \n Issues/gaps encountered?";
$('#Revue').val(revue_text_template);
或者,您可以使用javascript heredoc创建模板字符串,例如..
var revue_text_template = `Line 0
Line 1,
Line 2,
Line 3,
Line 4`;
答案 1 :(得分:1)
字符串只是一个普通的单行字符串,它被分割成多行。因此,它不包含\r
和/或\n
个字符。
使用 ES6 Template strings。
var revue_text_template = `Multi-Client impact?
Prime Speaker(s):
Component affected (infrastructure, application, server, network, etc.):
Root Cause identified? If yes, what is the cause?
How was the incident detected (alarm, client, vendor)?
Was the incident caused by a planned change? If yes, what is the change number?
Was recovery optimal? If not, why?
Issues/gaps encountered?`;
$('#Revue').val(revue_text_template);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<textarea class="form-control" rows="5" id="Revue" style="width: 100%; min-height: 200px; white-space: pre;">
</textarea>
&#13;
在 ES5 中,您可以使用Array#join
var revue_text_template = ["Multi-Client impact?",
"Prime Speaker(s):",
"Component affected(infrastructure, application, server, network, etc.):",
"Root Cause identified ? If yes, what is the cause ?",
"How was the incident detected(alarm, client, vendor) ?",
"Was the incident caused by a planned change ? If yes, what is the change number ?",
"Was recovery optimal ? If not, why ?",
"Issues / gaps encountered ?"
];
$('#Revue').val(revue_text_template.join("\n"));
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<textarea class="form-control" rows="5" id="Revue" style="width: 100%; min-height: 200px; white-space: pre;"></textarea>
&#13;
答案 2 :(得分:0)
在\ r \ n中添加了换行符。
var revue_text_template ="Multi-Client impact? \r\n\
Prime Speaker(s): \r\n\
Component affected (infrastructure, application, server, network, etc.): \r\n\
Root Cause identified? If yes, what is the cause? \r\n\
How was the incident detected (alarm, client, vendor)? \r\n\
Was the incident caused by a planned change? If yes, what is the change number? \r\n\
Was recovery optimal? If not, why? \r\n\
Issues/gaps encountered?";