换行符不会串联 [英] Line Break Won't Concatenate

查看:78
本文介绍了换行符不会串联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以网格格式打印9x9 2D JavaScript数组,但无法换行以连接成可打印的字符串.当我想在正方形网格中打印时,它将在一行中打印整个数组.这是代码:

I am trying to print a 9x9 2D JavaScript array in a grid format and I cannot get a line break to concatenate into a printable string. It prints the entire array in a single line when I want to print in a square grid. Here is the code:

function printBoard(board) {
    var toPrint = "";

    for (var i = 0; i < board.length; i++) {
        for (var j = 0; j < board.length; j++) {
         toPrint += board[i][j] + " ";
        }
        toPrint += "\n";
    }
    document.getElementById("printBoard").innerHTML = toPrint;
}

有人看到我看不到的明显东西吗?我是JavaScript的新手,因此,如果它是明显的错误,请对我轻松一点,但我不只是看到它. innerHTML函数不打印多行吗? HTML的一面非常简单:

Anyone see something obvious I'm not seeing? I'm new to JavaScript so take it easy on me if its an obvious error, but I'm not just seeing it. Does the innerHTML function not print multiple lines? The HTML side of it is very simple:

<p id="printBoard"></p> 

推荐答案

换行符在HTML中大部分时间都不起作用.您需要使用换行符:<br />,或将所有内容放入预格式化的元素:<pre></pre>

The newline character doesn't do anything most of the time in HTML . You need to use a linebreak element: <br />, or put everything in a preformatted element: <pre></pre>

https://developer.mozilla.org/en -US/docs/Web/HTML/Element/br

https://developer.mozilla.org/en -US/docs/Web/HTML/Element/pre

这篇关于换行符不会串联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆