我有这个非常棒的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:
<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>
答案 0 :(得分:1)
检查一下。创建另一个函数来显示信息,然后在页面加载时调用它。然后在单击按钮时计数器增量再次调用此函数。
<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>
请参阅此处jsfiddle
答案 1 :(得分:0)
在你的函数下添加clickCounter(),这样它就会在加载时执行。