猿问

帮忙看一段js代码

<div class="mainbody">

    <div class="text">

        <span>↑:上</span>

        <span>↓:下</span>

        <span>←:左</span>

        <span>→:右</span>

    </div>

    <div class="score">

        <span>

        <!-- i标签是斜体 -->

            得分: <i id="score">0</i>

        </span>

    </div>

<script type="text/javascript">

// 设置行数和列数

    var longness = 25;

    // writeIn能够在添加的参数后面添加一个换行符

    document.writeIn('<table>');

    for (var y=0;y<longness;y++){

        document.writeln('<tr>');

        for (var x=0;x<longness;x++){

            document.writeln('<td id="x'+x+'y'+y+'"></td>');

        }

        document.writeIn('</tr>');

    }

    document.writeIn('</table>')

</script>

</div>

不太明白将这段js代码放在div里是怎么起作用的。
尤其是document.writeln('<td id="x'+x+'y'+y+'"></td>');,这一段,里面添加的calss属性应该怎么理解。

慕少森
浏览 539回答 1
1回答

九州编程

你说的那一句里用的&nbsp;+&nbsp;有点多,看起来费劲,所以我把那句用 ES2015 来表述,看起来直观一点。// 设置行数和列数var longness = 25;// writeIn能够在添加的参数后面添加一个换行符document.writeIn("<table>");for (var y = 0; y < longness; y++) {&nbsp; &nbsp; document.writeln("<tr>");&nbsp; &nbsp; for (var x = 0; x < longness; x++) {&nbsp; &nbsp; &nbsp; &nbsp; document.writeln(`<td id="x${x}y${y}"></td>`);&nbsp; &nbsp; }&nbsp; &nbsp; document.writeIn("</tr>");}document.writeIn("</table>");然后就是你的问题,这里只有&nbsp;id&nbsp;属性,并没有&nbsp;class&nbsp;属性。如果要理解&nbsp;id,那就好解理了吧,这里就是为每个&nbsp;<td>&nbsp;赋予一个唯一的ID而已,也许后面会用到,也许不会。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答