简体   繁体   English

学习Javascript-两种选择都可以监听onclick事件的简便方法吗?

[英]Learning Javascript - easy way to listen to onclick event for both choices?

I'm learning javascript and I figured what best way to learn would be to make a game of TicTacToe (I made other very basic JavaScripts before this). 我正在学习javascript,我发现最好的学习方法是制作TicTacToe游戏(在此之前,我还制作了其他非常基本的JavaScript)。

I'm wondering if their is an easier solution then what I'm trying to do at the moment. 我想知道他们是否是一个更简单的解决方案,而不是我目前正在尝试做的事情。

I give the user an option if they want to be "x" or "o". 如果用户想成为“ x”或“ o”,我可以给他们一个选择。 I have two separate functions that fire off if they either choose "x" or "o". 我有两个单独的函数,如果它们选择“ x”或“ o”,它们就会触发。

Is there a way I can have just one function that can detect whether "x" or "o" was chosen then have another function begin right after that choice? 有没有一种方法可以让我有一个功能来检测是否选择了“ x”或“ o”,然后在选择之后立即启动另一个功能? (I'm thinking that it would be the function to draw out the tictactoe board). (我认为这将是抽签板的功能)。

You can see I was playing around with the var userChoiceO=document.GetElementById("O"); 您可以看到我正在使用var userChoiceO=document.GetElementById("O"); and w .addEventListener("click", userChoice); 和w .addEventListener("click", userChoice); but I am still learning and some of this is going over my head. 但我仍在学习,其中一些正在困扰我。

Any kind of advice would be kindly appreciated if I'm going in the right direction with this. 如果我朝正确的方向前进,任何建议都将不胜感激。

Here is a general idea of what I got so far: 这是到目前为止我得到的一般想法:

 var userShape = 'X'; var playerX=document.getElementById("X"); var playerO=document.getElementById("O"); function userChoiceX () { userShape = 'X'; } function userChoiceO() { userShape = '0'; } 
 .XO { font-size:70px; text-align: center; } #X { color:#ff574f; cursor: pointer; text-align: center; margin-right: 6vw; -webkit-transition: all 0.3s ease; -moz-transition: all 0.3s ease; -o-transition: all 0.3s ease; -ms-transition: all 0.3s ease; } #X:hover { font-size:100px; } #O { color:#8cabec; cursor: pointer; text-align: center; margin-left: 6vw; -webkit-transition: all 0.3s ease; -moz-transition: all 0.3s ease; -o-transition: all 0.3s ease; -ms-transition: all 0.3s ease; } #O:hover { font-size:100px; } 
  <p>Click on the shape you would like to play as</p> <div class="XO"> <span id="X" class="X" onclick="userChoiceX()">X</span><span id="O" class="O" onclick="userChoiceO()">O</span> </div> 

You can bind the event handler with the parent of the two spans <div class="XO" onclick="userChoice(event);"> , 您可以将事件处理程序与两个跨度的父<div class="XO" onclick="userChoice(event);">绑定<div class="XO" onclick="userChoice(event);">

then , event.target gives you the clicked span element: 然后, event.target为您提供单击的span元素:

   function userChoice(event) {
      if (event.target.id == 'X')
        return userChoiceX();
      else if (event.target.id == 'O')
       return userChoiceO();
   }

 var userShape = 'X'; var players=document.querySelector(".XO"); function userChoice(event) { if (event.target.id == 'X') return userChoiceX(); else if (event.target.id == 'O') return userChoiceO(); } function userChoiceX () { userShape = 'X'; } function userChoiceO() { userShape = '0'; } 
 .XO { font-size:70px; text-align: center; } #X { color:#ff574f; cursor: pointer; text-align: center; margin-right: 6vw; -webkit-transition: all 0.3s ease; -moz-transition: all 0.3s ease; -o-transition: all 0.3s ease; -ms-transition: all 0.3s ease; } #X:hover { font-size:100px; } #O { color:#8cabec; cursor: pointer; text-align: center; margin-left: 6vw; -webkit-transition: all 0.3s ease; -moz-transition: all 0.3s ease; -o-transition: all 0.3s ease; -ms-transition: all 0.3s ease; } #O:hover { font-size:100px; } 
  <p>Click on the shape you would like to play as</p> <div class="XO" onclick="userChoice(event);"> <span id="X" class="X" >X</span><span id="O" class="O" >O</span> </div> 

Use event delegation with document.addEventListener() and event.target . 事件委托document.addEventListener()event.target一起使用 That way the HTML markup doesn't have JavaScript mixed-in (aka Unobtrusive Javascript ), and there can be fewer issues with memory leaks that way (eg in case elements with event handlers are removed). 这样,HTML标记中就不会混入JavaScript(也称为Unobtrusive Javascript ),并且通过这种方式可以减少内存泄漏的问题(例如,删除带有事件处理程序的元素)。 The example below also uses Element.className and String.indexOf() to check the class name of the element clicked on. 下面的示例还使用Element.classNameString.indexOf()来检查单击的元素的类名称。

 //wait until the DOM is ready document.addEventListener('DOMContentLoaded', function(domReadyEvent) { //observe clicks on the document document.addEventListener('click', function(clickEvent) { if (clickEvent.target.className.indexOf('X') > -1) { console.log('clicked on X'); //handle click on X } else if (clickEvent.target.className.indexOf('O') > -1) { console.log('clicked on O'); //handle click on O } }); }); 
 .XO { font-size: 70px; text-align: center; } #X { color: #ff574f; cursor: pointer; text-align: center; margin-right: 6vw; -webkit-transition: all 0.3s ease; -moz-transition: all 0.3s ease; -o-transition: all 0.3s ease; -ms-transition: all 0.3s ease; } #X:hover { font-size: 100px; } #O { color: #8cabec; cursor: pointer; text-align: center; margin-left: 6vw; -webkit-transition: all 0.3s ease; -moz-transition: all 0.3s ease; -o-transition: all 0.3s ease; -ms-transition: all 0.3s ease; } #O:hover { font-size: 100px; } 
 <p>Click on the shape you would like to play as</p> <div class="XO"> <span id="X" class="X">X</span><span id="O" class="O">O</span> </div> 

You can use the value as an argument.. 您可以将值用作参数。

make one function called userChoice() and make it such that it's turnwise using a variable turn 创建一个名为userChoice()函数,并使用可变的turn使其顺时针turn

so your function becomes 所以你的功能变成

turn = 'X'
function userChoice(){
   userShape = turn
   if(turn == 'X'){
     turn = 'Y'
   }
   else{
     turn = 'X'
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM