Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Javascript到HTML表示换行符_Javascript_Html - Fatal编程技术网

从Javascript到HTML表示换行符

从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

我目前正在使用Javascript从数组构建一个字符串,并将其传递给HTML模式,但是我不清楚如何显示中间有换行符的字符串,因为现在HTML将Javascript中构建的字符串表示为一行。任何帮助都将不胜感激

Javascript:

    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">&times;</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;
    }
    
  • 使用str.replace(/\n/g,“
    ”)将换行符替换为
    标记

  • 我想这会解决你的问题

    函数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