Attaching event handler to DOM elements

前端 未结 6 713
[愿得一人]
[愿得一人] 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 12:50

    Try:

    document.getElementById('one').onclick();
    

    For binding the event handler in js file:

    var board = document.getElementById('board'),
          divs = board.getElementsByTagName('div'),
          i, len = divs.length;
    for (i = 0; i < len; i++) {
          divs[i].onclick = function() {
               playerMove(this); 
          };
    }
    

提交回复
热议问题