Javascript Object for Tic Tac Toe game

后端 未结 3 1735
星月不相逢
星月不相逢 2021-01-29 09:21

Complete newbie here on the Javascript topic. I am trying to code out a Tic Tac Toe game, while using JS\' Objects to save code space. I figured I might create a JS Object which

3条回答
  •  自闭症患者
    2021-01-29 10:09

    Separate you swap function out of your object and just get rid of the object all together. Add a class to all of the HTMLElements you wish to add an EventListener to and then query them all using the following:

    function swap(element) { if (element.textContent === ""){ element.textContent = "X"; }else if (element.textContent === "X") { element.textContent = "O"; }else { element.textContent = ""; } } document.querySelectorAll('.your-class').forEach(function (element) { element.addEventListener('click', swap(element)); });

提交回复
热议问题