Change of variable value



  • There's a variable. document.turn = "Text 1"♪ I need her to matter ♪ Text 1 Changed consecutively Text 2♪ I mean, first use. document.turnText 12nd - Text 2♪ I hope you can help me, thank you very much in advance.

    <head>
        <script>
            function onClick(sender) {
                document.turn = "X"
                if (sender.innerText == ""){
                  sender.innerHTML = document.turn
                }
            }
        </script>
    </head>
    <body>
        <table frame="void" align="center">
            <tbody>  
            <tr>
                <td id="c1" class="Square" onclick="onClick(this);"></td>
                <td id="c2" class="Square" onclick="onClick(this);"></td>
                <td id="c3" class="Square" onclick="onClick(this);"></td>
            </tr>
            <tr>
                <td id="c4" class="Square" onclick="onClick(this);"></td>
                <td id="c5" class="Square" onclick="onClick(this);"></td>
                <td id="c6" class="Square" onclick="onClick(this);"></td>
            </tr>
            <tr>
                <td id="c7" class="Square" onclick="onClick(this);"></td>
                <td id="c8" class="Square" onclick="onClick(this);"></td>
                <td id="c9" class="Square" onclick="onClick(this);"></td>
            </tr>
            </tbody>
        </table>
    </body>
    


  • In this case, the branches are sufficient else where there will be a change between x and o

    function onClick(sender) {
        var steps = {"X":"O", "O":"X"}; //порядок смен, после X->O, после O->X
        if (sender.innerText == ""){
          sender.innerHTML = "X";//если первый раз - устанавливаем X
        }else{
          sender.innerHTML = steps[sender.innerText];
        }
    }
    

    UPDATE: for comment

    var steps = {"X":"O", "O":"X"}; //порядок смен, после X->O, после O->X
    var current = "X";
    

    function onClick(sender) {
    if (sender.innerText == ""){// если пустая ячейка
    sender.innerHTML = current; //устанавливаем текущее значение
    current = steps[current];//меняем на следующее
    }
    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2