1

当我按Ctrl+A突出显示从中绘制的所有画布时,最后一行似乎是空白的或全部组合在一起。我正在尝试制作一个 4x4 的画布网格。我的逻辑错误是什么?

var count = 0;
while (count < 16) {
    if (count % 4 == 0)
        document.write("<br><canvas id = \"canvas_ \" + count + \" width = \"50\" height = \"     50\"></canvas>");
    else
        document.write("<canvas id = \"canvas_ \" + count + \" width = \"50\" height = \" 50\"></canvas>");
    count++;
}
4

2 回答 2

1

我已经重写了你的代码:

var count = 0;
while (count < 16) {
    if (count % 4 == 0)
        document.write('<br>');
    document.write('<canvas id="canvas_' + count + '" width="50" height="50"></canvas>');
    count++;
}
document.write('<br>');

除了改进编码风格的方式(例如使用两种引号)之外,我在<br>最后添加了一个,因为这就是导致最后一行未被选中的原因。

添加 acanvas { border: 1px solid #000 }使其看起来像这样:

一个

于 2013-03-21T17:14:43.307 回答
0

Your code is ok but it seams that canvas element is not selected when you press CTRL+A only <br/> elements. It will work when you add one more <br/> at the end. And you have also errors in your code count was inside a string.

var count = 0;
document.write("<br>");
while (count < 16) {
    document.write("<canvas id=\"canvas_" + count + "\" width=\"50\" height=\"50\"></canvas>");
    count++;
    if (count % 4 == 0) {
        document.write("<br>");
    }
}
于 2013-03-21T17:26:46.283 回答