简体   繁体   English

如何让我的 JavaScript 计数器显示?

[英]How do I get my JavaScript counter to show?

I have this great JavaScript counter that works perfect.我有一个完美的 JavaScript 计数器。 The only issue is that the actual information doesn't show up until the button is clicked first.唯一的问题是,直到首先单击按钮后,实际信息才会显示。 But I want the counter & other text to constantly display and update as the buttons are being clicked.但是我希望计数器和其他文本在单击按钮时不断显示和更新。

What am I missing here?我在这里缺少什么?

JavaScript: JavaScript:

<script>
function clickCounter() {
    if(typeof(Storage) !== "undefined") {
        if (localStorage.clickcount) {
            localStorage.clickcount = Number(localStorage.clickcount)+1;
        } else {
            localStorage.clickcount = 1;
        }
        document.getElementById("result").innerHTML = "" + localStorage.clickcount + " <br>Subscribers So Far! And...<br>" + (1500 - +localStorage.clickcount) + "<br>Subscribers To Go" ;
    } else {
        document.getElementById("result").innerHTML = "Sorry, your browser does not support web storage...";
    }
}
</script>

HTML: HTML:

<p><button onclick="clickCounter()" id="paypal-btn1" type="button">Click me!</button></p>
<p><button onclick="clickCounter()" id="paypal-btn2" type="button">Click me!</button></p>
<p><button onclick="clickCounter()" id="paypal-btn3" type="button">Click me!</button></p>
<div id="result"></div>

Check this out.看看这个。 Create another function to display the information then call it when the page load.创建另一个函数来显示信息,然后在页面加载时调用它。 Then call this function again when the counter increment when clicked the button.然后当单击按钮时计数器增加时再次调用此函数。

<script>
    displayInformation(); 
    function displayInformation() {
            document.getElementById("result").innerHTML = "" + localStorage.clickcount + " <br>Subscribers So Far! And...<br>" + (1500 - +localStorage.clickcount) + "<br>Subscribers To Go" ;
    }
    function clickCounter() {
        if(typeof(Storage) !== "undefined") {
            if (localStorage.clickcount) {
                localStorage.clickcount = Number(localStorage.clickcount)+1;
            } else {
                localStorage.clickcount = 1;
            }
        displayInformation();/*Call this to refresh the click information*/
        } else {
            document.getElementById("result").innerHTML = "Sorry, your browser does not support web storage...";
        }
    }
</script>

See here jsfiddle看这里jsfiddle

在您的函数下添加 clickCounter() 以便它将在加载时执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在我的 javascript 游戏中添加计数器? - How do I add a counter in my javascript game? 如何让 div 在 javascript 中显示? - How do I get the divs to show in javascript? 如何使用以前的方法在CSS中显示CSS类? - How do I get a CSS class to show up in my javascript with my previous method? 如何使用angular和javascript使我的mlab数据库显示在我的html页面上? - How do I get my mlab db to show up on my html page using angular and javascript? 我需要向我的 Javascript 添加什么才能显示来自我的 DynamoDB 表和 API 网关的命中计数器? - What do I need to add to my Javascript to be able to show the hit counter from my DynamoDB table and API Gateway? 如何在此 Javascript 计数器中添加逗号? - How do I add commas to this Javascript counter? 如何从本地存储中获取特定数据,以便在 JavaScript 中以模态显示它们? - How do I get specific data from my localstorage so I can show them in a modal in JavaScript? 如何在同一页面上的2个div中显示我的JavaScript计数器结果? - How to show my javascript counter result in 2 divs on the same page? 如何使我的Javascript仅显示一个集中在鼠标上的聚光灯? - How do I get my Javascript to show just one spotlight centred on the mouse? 如何使用JavaScript进行计数器 - How to do a counter with JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM