HTML / Javascript按钮点击计数器 [英] HTML/Javascript Button Click Counter

查看:2208
本文介绍了HTML / Javascript按钮点击计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前做过类似的事情,我知道这是真的很接近。我只是试图让我的按钮增加javascript变量,然后函数显示新的值。

I have done something similar to this before, and I know this is really close. I'm just trying to make it so that my button increments the javascript variable, and the function then displays the new value.

<html>
<head>
    <title>Space Clicker</title>
</head>

<body>
    <script type="text/javascript">
    int clicks = 0;
    function click() {
        clicks += 1;
        document.getElementById("clicks").innerHTML = clicks;
    };
    </script>
    <button type="button" onClick="click()">Click me</button>
    <p>Clicks: <a id="clicks">0</a></p>

</body></html>


推荐答案

使用 var 整数代替您的点击变量生成,onClick而不是点击作为函数名称:

Use var instead of int for your 'clicks' variable generation and 'onClick' instead of 'click' as your function name:

<html>
<head>
    <title>Space Clicker</title>
</head>

<body>
    <script type="text/javascript">
    var clicks = 0;
    function onClick() {
        clicks += 1;
        document.getElementById("clicks").innerHTML = clicks;
    };
    </script>
    <button type="button" onClick="onClick()">Click me</button>
    <p>Clicks: <a id="clicks">0</a></p>

</body></html>

在JavaScript变量中使用var标签声明,没有像int,bool, string ...声明变量。您可以使用'typeof(yourvariable)'获取变量的类型,并在Google上找到更多支持。

In JavaScript variables are declared with the 'var' tag, there are no tags like int, bool, string... to declare variables. You can get the type of an variable with 'typeof(yourvariable)', more support about this you find on Google.

名称click由JavaScript保留对于函数名,因此您必须使用其他任何东西。

And the name 'click' is reserved by JavaScript for functionnames so you have to use anything else.

这篇关于HTML / Javascript按钮点击计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆