为什么计数器没有增加

时间:2018-07-24 02:48:57

标签: javascript html html5

请帮我解决这个问题,我被困在这里,我是JavaScript新手,这就是为什么我无法解决这个简单错误

 <!DOCTYPE html>
    <html>
        <head>
            <title>Page Title</title>

            <script>
                var scr1 = 0;
    var scr2 = 0;
    var counter = 0;

    window.onload = function () {
    var YS = document.getElementById("YS");
    var CS = document.getElementById("CS");
    var mid = document.getElementById("mid");

    YS.innerHTML = "Your Score :  " + scr1 + counter  ;
    CS.innerHTML = "Com Score  :  " + scr2 ;

     }


    function srt() {
         counter++;
    }
            </script>
        </head>
        <body>
        <h2 class="scr1" id="YS">Your Score :</h1>
        <h2 class="scr2" id="CS">Com Score :</h2>
        <br />
        <br />
        <center>
        <div class="mid" id="mid"><p>0</p></p></div>
        <div class="srt" id="none" onclick="srt()">
            <h4>Start</h4>
        </div>
        </center>

        </body>
    </html>
  

希望您能理解我的问题,如果可以,请帮助我

谢谢

2 个答案:

答案 0 :(得分:1)

您的计数器在增加,但是当它增加时您没有显示更改。

 <!DOCTYPE html>
<html>
    <head>
        <title>Page Title</title>
        <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
        <meta content="utf-8" http-equiv="encoding">
        <script>
        var scr1 = 0;
        var scr2 = 0;
        var counter = 0;

        window.onload = function () {
            var YS = document.getElementById("YS");
            var CS = document.getElementById("CS");
            var mid = document.getElementById("mid");

        }


        function srt() {
            console.log(counter);
            counter++;

            YS.innerHTML = "Your Score :  " + scr1 + counter  ;
            CS.innerHTML = "Com Score  :  " + scr2 ;
        }
        </script>
    </head>
    <body>
    <h2 class="scr1" id="YS">Your Score :</h1>
    <h2 class="scr2" id="CS">Com Score :</h2>
    <br />
    <br />
    <center>
    <div class="mid" id="mid"><p>0</p></p></div>
    <div class="srt" id="none" >
        <button onclick="srt()">Start</button>
    </div>
    </center>

    </body>
</html>

下次,使用 console.log()进行调试

答案 1 :(得分:0)

在“开始div”上单击,您需要更新其他Div,以显示更新后的值。

<!DOCTYPE html>
    <html>
        <head>
            <title>Page Title</title>

            <script>
                var scr1 = 0;
    var scr2 = 0;
    var counter = 0;

    window.onload = function () {
    var YS = document.getElementById("YS");
    var CS = document.getElementById("CS");
    var mid = document.getElementById("mid");

    YS.innerHTML = "Your Score :  " + scr1 + counter  ;
    CS.innerHTML = "Com Score  :  " + scr2 ;

     }


    function srt() {
         counter++;
      mid.innerHTML = counter;
      YS.innerHTML = "Your Score :  " + scr1 + counter  ;
      CS.innerHTML = "Com Score  :  " + scr2 ;
    }
            </script>
        </head>
        <body>
        <h2 class="scr1" id="YS">Your Score :</h1>
        <h2 class="scr2" id="CS">Com Score :</h2>
        <br />
        <br />
        <center>
        <div class="mid" id="mid"><p>0</p></p></div>
        <div class="srt" id="none" onclick="srt()">
            <h4>Start</h4>
        </div>
        </center>

        </body>
    </html>