从Javascript到HTML表示换行符
我目前正在使用Javascript从数组构建一个字符串,并将其传递给HTML模式,但是我不清楚如何显示中间有换行符的字符串,因为现在HTML将Javascript中构建的字符串表示为一行。任何帮助都将不胜感激 Javascript:从Javascript到HTML表示换行符,javascript,html,Javascript,Html,我目前正在使用Javascript从数组构建一个字符串,并将其传递给HTML模式,但是我不清楚如何显示中间有换行符的字符串,因为现在HTML将Javascript中构建的字符串表示为一行。任何帮助都将不胜感激 Javascript: function ShowCrashString(str) { var temp_split = str.split(';'); temp_str = ''; for (var i=0; i < temp_split.leng
function ShowCrashString(str) {
var temp_split = str.split(';');
temp_str = '';
for (var i=0; i < temp_split.length; i++) {
temp_str += temp_split[i] + '\n;'
}
document.getElementById("crash-modal").innerHTML = temp_str;
}
函数ShowCrashString(str){
var temp_split=str.split(“;”);
temp_str='';
对于(变量i=0;i
HTML模式代码:
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Crash Analysis</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="crash-modal">
<!-- data gets populated here -->
</div>
<div class="modal-footer" id="to-copy">
<button type="button" class="btn btn-secondary" data-dismiss="modal"
id="closeModal">Close</button>
<button class="btn btn-primary" onClick="CopyToClipboard('crash-modal')">Copy</button>
</div>
</div>
</div>
</div>
碰撞分析
&时代;
接近
复制
您需要使用
来代替HTML的\n
temp_str += temp_split[i] + '<br/>'
temp_str+=temp_split[i]+'
有两种方法可以做到这一点:
function ShowCrashString(str) {
var temp_split = str.split(';');
temp_str = '';
for (var i=0; i < temp_split.length; i++) {
temp_str += temp_split[i] + '<br/>'
}
document.getElementById("crash-modal").innerHTML = temp_str;
}
”)将换行符替换为
标记
我想这会解决你的问题
函数ShowCrashString(str){
var temp_split=str.split(“;”);
temp_str='';
对于(变量i=0;i
}
document.getElementById(“崩溃模式”).innerHTML=temp_str;
}
temp\u str+=temp\u split[i]+“
”
。因为它是HTML,所以请使用
标记而不是\n