JavaScript 未捕获类型错误 : Cannot set property '-1' of undefined

标签 javascript jquery html arrays tic-tac-toe

我正在练习 jquery,并尝试构建一个非常基本的 Tic Tac Toe 游戏。

我想将我的 html 表设置为相当于数组形式的网格索引,并根据玩家的点击更改它们的值。目前,我的颜色变化取决于轮流工作,但我不断收到此 Uncaught TypeError,引用条件语句中的 board[row][col] = x 行。

我的代码:

$(document).ready(function(event) {

    var count = 0;

    var board = [
        [0, 0, 0], 
        [0, 0, 0], 
        [0, 0, 0]
    ];

    var row = $(this).parent().index();
  var col = $(this).index();

    $('td').click(function() {
        // if count is even, player 1 is yellow
        // if count is odd, player 2 is blue
        if (count % 2 === 0) {
            $(this).addClass('yellow');
            count++;
            board[row][col] = 1;
        } else {
            $(this).addClass('blue');
            count++;
            board[row][col] = 2;
        }
    });
});

相关html:

<div id="main_container">
    <h1 id="main_heading" class="heading" >Tic! Tac! Toe!</h1>
    <h2 id="winning"></h2>
    <table>
        <tbody>
            <tr class="box_row" >
                <td id="box_0_0" class="box_cell" data-row="0" data-col="0" data-clicked="0"></td>

                <td id="box_0_1" class="box_cell" data-row="0" data-col="1" data-clicked="0"></td>

                <td id="box_0_2" class="box_cell" data-row="0" data-col="2" data-clicked="0"></td>
            </tr>
            <tr class="box_row">
                <td id="box_1_0" class="box_cell" data-row="1" data-col="0" data-clicked="0"></td>

                <td id="box_1_1" class="box_cell" data-row="1" data-col="1" data-clicked="0"></td>

                <td id="box_1_2" class="box_cell" data-row="1" data-col="2" data-clicked="0"></td>
            </tr>
            <tr class="box_row">
                <td id="box_2_0" class="box_cell" data-row="2" data-col="0" data-clicked="0"></td>

                <td id="box_2_1" class="box_cell" data-row="2" data-col="1" data-clicked="0"></td>

                <td id="box_2_2" class="box_cell" data-row="2" data-col="2" data-clicked="0"></td>
            </tr>
        </tbody>
    </table>
</div>

相关CSS:

.yellow {
  background-color: #ffc300;
}

.blue {
  background-color: #73d2c9;
}

最佳答案

问题是您在哪里读取索引值,您需要在点击处理程序中读取它

$(document).ready(function (event) {

    var count = 0;

    var board = [
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]
    ];

    //here this is the document object
    $('td').click(function () {

        //reading index values should be inside the click handler, here `this` refers to the clicked `td` element
        var row = $(this).parent().index();
        var col = $(this).index();


        // if count is even, player 1 is yellow
        // if count is odd, player 2 is blue
        if (count % 2 === 0) {
            $(this).addClass('yellow');
            count++;
            board[row][col] = 1;
        } else {
            $(this).addClass('blue');
            count++;
            board[row][col] = 2;
        }
    });
});

关于JavaScript 未捕获类型错误 : Cannot set property '-1' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26643322/

相关文章:

使用 Onclientclick 或 Onsubmit 都不会调用 JavaScript 函数

javascript - Youtube 在手机上嵌入自动播放

javascript - Angular 循环多维 json 对象不起作用

python - 在带有 ipywidgets 的 jupyter notebook 上使用 nbconvert 时,输出小部件出现在选项卡小部件之外

javascript - 如何获取 D3.js 饼图以从 DOM 动态呈现数据

javascript - 同步Window.show()

javascript - 如何在旧版本浏览器中获取输入文件的数据

javascript - 连接激活的两个 HTML 菜单

php - 如何修复出现拉伸(stretch)的图像?

html - 使 HTML div 更棒的方式