如何使按钮图像在单击时添加一个到变量?

How do I make a button image add one to a variable on click?

如何让按钮图像在点击时更改变量 +1? ~约翰

我想这就是你要的:

<html>
<body>
<button onclick="addOne()">ADD</button><br/>
<button onclick="getCounter()">PRINT</button>
</body>
<script>
    var counter = 0;
    function addOne(){
        counter++;
    }

    function getCounter(){
        console.log(counter);
    }
</script>
</html>

使用诸如 onclick 事件之类的东西,您可以调用函数来增加变量的值。