简体   繁体   English

为什么我的条件不做任何事情?

[英]Why isn't my conditional doing anything?

I'm making a simple tic-tac-toe game but for some reason when trying to create the rules for the game, I can't seem to get anything to work. 我正在制作一个简单的井字游戏,但是由于某些原因,当尝试创建游戏规则时,我似乎什么也无法工作。 I have a multidimensional array that has the rules in it but I can't even get one cell to react accordingly. 我有一个包含规则的多维数组,但是我什至无法让一个单元格做出相应的反应。 What's wrong with the bottom if statement in my JavaScript pannel? JavaScript面板中的底部if语句有什么问题? http://jsfiddle.net/Cp4Lu/1/ (This is the if statement I'm referring to) http://jsfiddle.net/Cp4Lu/1/ (这是我指的if语句)

if (c9.text === 'X') {
alert("You win!");
}

You have a couple problems 你有几个问题

Uncaught ReferenceError: c9 is not defined 
  • You're using c9 outside of the scope in which it is defined 您正在c9定义范围之外使用
  • The if (c9... isn't inside any event listener , so it only happens the first time when the JavaScript runs initially if (c9...不在任何事件侦听器中 ,因此仅在JavaScript首次运行时才发生
  • jQuery's text is a function , not a property jQuery的text是一个函数 ,而不是一个属性
  • an id in HTML should begin with a letter HTML中ID 应该以字母开头

Here you go: http://jsfiddle.net/Cp4Lu/4/ 您可以在这里: http : //jsfiddle.net/Cp4Lu/4/

I basically put all your code in the $(document).ready and put your conditional within your click event. 我基本上将所有代码都放在$(document).ready中,并将条件放在click事件中。 I believe it now does what you were looking for. 我相信它现在可以满足您的需求。

$('td').on('click', function () {
    turnCount += 1;
    setCurrentPlayer();
    $(this).text(currentPlayer);
    console.log(turnCount);
    if (c9.text() === 'X') {
        alert("You win!");
    }
});

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

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