【问题标题】:JavaScript - Count how many time a button is pressed(function)JavaScript - 计算按下按钮的次数(函数)
【发布时间】:2015-04-18 12:48:25
【问题描述】:

我试图计算一个按钮被按下了多少次,但我认为这是不对的,因为即使没有点击“打招呼”按钮,计数按钮也会不断增加

有什么想法吗?

<!DOCTYPE html>

<html>
    <head>
        <title>JSExample</title>
    </head>

    <body>

        <script type="text/javascript">
            function hello() {
               alert("Hello there!");
            }

            var countClicks = 0;

            function upCount() { 

                if(document.getElementById('hello').onclick = "hello()") {
                    countClicks++;
                    alert(countClicks);
                    return true;
                } else {
                    return false;
                }

            }

        </script>

        <p><input type="button" name="button" value="Saying Hello" id="hello" onclick="hello();"/></p>

        <p><input type="button" name="click" value="Counting" id="click" onclick="upCount();"/></p>

    </body>

</html>

【问题讨论】:

    标签: javascript html button onclick


    【解决方案1】:

    Y,我不太明白你的问题。现在点击“打招呼”,您正在调用一个函数“hello()”,它只会弹出一个带有“Hello there”的警报。单击“计数”按钮时,您正在检查一个我不完全理解的非常奇怪的条件,然后增加值。因此,如果您想计算“say hello”被点击了多少次,然后用“Counting”按钮显示,请使用此代码。

    <!DOCTYPE html>
    

     <html>
            <head>
                <title>JSExample</title>
            </head>
        
            <body>
        
                <script type="text/javascript">
                    function hello() {
                       alert("Hello there!");
        			   countClicks++;
                    }
        
                    var countClicks = 0;
        
                    function upCount() { 
        				alert(countClicks);
                    }
        
                </script>
        
                <p><input type="button" name="button" value="Saying Hello" id="hello" onclick="hello();"/></p>
        
                <p><input type="button" name="click" value="Counting" id="click" onclick="upCount();"/></p>
        
            </body>
        
        </html>

    如果这不是你的意思,请告诉我:)

    【讨论】:

    • 是的,这正是我想要的,非常感谢!看来我没有用“Hello There”调用来调用 CountClicks 变量
    【解决方案2】:

    条件if(document.getElementById('hello').onclick = "hello()") { 不正确。我不确定它应该做什么,但知道它做什么:它将字符串hello() 分配给hello 元素的.onclick 回调。由于无法执行String(与Function 相反),它有效地删除了回调。你可能想要这样的东西

    var countClicks = 0;
    
    function hello() {
       alert("Hello there!");
       countClicks++;
    }
    
    function upCount() { 
        alert(countClicks);
    }
    <p><input type="button" name="button" value="Saying Hello" id="hello" onclick="hello();"/></p>
    <p><input type="button" name="click" value="Counting" id="click" onclick="upCount();"/></p>

    【讨论】:

      【解决方案3】:
      if(document.getElementById('hello').onclick == "hello()")
      

      你错过了一个 =

      【讨论】:

        猜你喜欢
        • 2014-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多