[英]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>
在您的函数下添加 clickCounter() 以便它将在加载时执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.