【问题标题】:HTML table for chess coordinates国际象棋坐标的 HTML 表
【发布时间】:2016-01-06 21:10:32
【问题描述】:

您好,我必须创建一个 HTML 表格作为棋盘,通过在它们所在的单元格中显示 K 和 Q 来显示国王和王后的位置。

我原来的脚本是这样的;

if (kingX==queenX || kingY==queenY){
    alert('In Check!');
}
    else if (Math.abs(kingX - queenX)==Math.abs(kingY - queenY)){
        alert('In Check!');
    }
        else{
            alert('Not in Check!');
        }

【问题讨论】:

标签: html-table


【解决方案1】:

您可以使用document.createElement() 创建元素并使用appendChild() 将这些元素附加到其他现有元素(也就是最后一个子元素):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

<script type="text/javascript">
    var i, j;
    var table = document.createElement('table');
    document.body.appendChild(table)
    for (i = 0; i < 5; i++) {
        var row = document.createElement('tr');
        table.appendChild(row);
        for (j = 0; j < 5; j++) {
            var cell = document.createElement('td');
            cell.textContent = i * 5 + j;
            cell.style.border = 'thin solid black';
            row.appendChild(cell);
        }
    }
</script>
</body>
</html>

【讨论】:

    猜你喜欢
    • 2014-03-02
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    相关资源
    最近更新 更多