【问题标题】:Increment button not working增量按钮不起作用
【发布时间】:2016-07-01 08:28:40
【问题描述】:

Js

//Called when application is started.
function OnStart()
{
// Set counter default to zero
var $counter = 0;

function increase()
{
$counter++;
return false;
 }};

HTML

 <div id="counter">0</div>

<img src="Chop.png" alt="some_text" id="chopper">
<img src="monster1.png" alt="mon" id="monster1">
<img src="shot.png" alt="bang" id="shot">

<button type="button" class="btn btn-danger btn-circle" id="fire" onClick="counter++;"><p>123</p>

我只是想制作一个基本的增量按钮,因为没有出现错误消息,所以真的看不到出了什么问题。

我基本上是在尝试制作一个非常基本的 html 游戏,您可以在其中使用按钮开火,因此从怪物生活中获得 -1...

谢谢

【问题讨论】:

    标签: increment


    【解决方案1】:

    要改变id为“counter”的div的内容,你必须调用document.getElementById("counter").innerHTML = &lt;new_value&gt;

    <script>
    function onStart() 
    { 
        counter = 0; 
    } 
    
    function increase() 
    { 
        counter++; 
        document.getElementById("counter").innerHTML = counter; 
    }  
    </script> 
    
    <div id="counter">0</div> 
    
    <img src="Chop.png" alt="some_text" id="chopper"> 
    <img src="monster1.png" alt="mon" id="monster1">
    <img src="shot.png" alt="bang" id="shot"> 
    
    <button type="button" class="btn btn-danger btn-circle" id="fire" onclick="increase();"><p>123</p> 
    

    【讨论】:

    • 非常感谢,但我从柜台得到一个 NaN 代码,这是因为它在 html 和 JS 中被引用了吗?
    • 可能是因为您有时使用$counter 而不是counter?这里不需要美元符号。
    猜你喜欢
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    相关资源
    最近更新 更多