Attaching event handler to DOM elements

前端 未结 6 658
[愿得一人]
[愿得一人] 2021-01-28 12:30

I am working on a tic tac toe game, which is almost complete. The only thing I am left wondering about is if it is possible to add an event handler for onclick fro

6条回答
  •  走了就别回头了
    2021-01-28 13:04

    You should really consider using jQuery for this. If you were using jQuery, this would have been as simple as:

    $('#board > div').click(playerMove);
    

    In case you want to stick with vanilla JS, you can do:

    var items = document.getElementById('board').children;
    for(x in items) {
        items[x].onclick = function() {
            playerMove(items[x]);
        };
    }
    

提交回复
热议问题