如何通过点击一个按钮来添加1至一个javascript变量? [英] How to add 1 to a javascript variable by clicking a button?

查看:181
本文介绍了如何通过点击一个按钮来添加1至一个javascript变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有编写JavaScript,我需要帮助拼命很少的经验。我需要这样一个按钮被点击时,比分被一个类似于饼干唱首歌游戏是如何工作的,这是一个分配上升,最终产品是类似于饼干唱首歌游戏的游戏。

这是code我至今;

 <!DOCTYPE HTML>
< HTML和GT;< HEAD>
    <标题>分配和LT; /标题>< /头><身体GT;
    <报头GT;
        <强>不要触摸按钮< / STRONG>
    < /头>    < D​​IV CLASS =游戏对象>
        <脚本类型=文/ JavaScript的>
            VAR点击= 0;
        < / SCRIPT>
        <按钮式=按钮的onClick =点击++ID =推>没有。 < /按钮>
    < / DIV>


解决方案

您code已经工作。 这里有一个演示演示您的code在行动。

我添加了一个函数来显示点击数:

 函数updateClickCount(){
    的document.getElementById(的clickCount)的innerHTML =点击。
}

I have very little experience with writing javascript and I need help desperately. I need to make so that when a button is clicked the score goes up by one similar to how the cookie clicker game works this is for an Assignment, the end product is to be a game similar to the cookie clicker game.

this is the code I have so far;

<!DOCTYPE html>
<html>

<head>
    <title> Assignment </title>

</head>

<body>
    <header>
        <strong>Don't Touch The Button</strong>
    </header>

    <div class="game-object">
        <script type="text/javascript">
            var clicks = 0;
        </script>
        <button type="button" onClick ="clicks++" id="push"> NO. </button>
    </div>

解决方案

Your code already works. Here's a demo that demonstrates your code in action.

I have added a function to display the click count:

function updateClickCount() {
    document.getElementById("clickCount").innerHTML = clicks;
}

这篇关于如何通过点击一个按钮来添加1至一个javascript变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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