I want to check if $('#td1').text() === “x”?

后端 未结 5 1401
余生分开走
余生分开走 2021-01-28 16:10

I want to check if innerHtml have X or O , so i can not add again any thing else , but it\'s not working . it stop after adding the check code , I\'m trying here to do a simple

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-28 17:05

    Your code is very hard to read. I've whipped a quick almost working script for you to read and understand http://jsfiddle.net/mendesjuan/MQ9Nr/

    However, it seems that for what you need, you just have to check if the text is empty or not. Here are a few suggestions

    • Don't set handlers from HTML
    • Don't need to pass a number into the handler, the handler knows what td was clicked

    JS

    $(function(){
      var turn = 0;
      var xos = ["x", "o"];
      $("td").click(function(){
        var $this = $(this);
          if ($this.text()) {
            return;
          } else {
            $this.text(xos[turn]);
            turn = (turn + 1) % 2;
          }
      });
    });
    

    HTML

    X ,O Game

提交回复
热议问题