![](/img/trans.png)
[英]When I Sometimes click on shopping button , it does not work javascript
[英]Why does JavaScript sometimes function not work when button is clicked?
我有一个 JavaScript 投票系统,它有三个按钮:
投票赞成、清除和投票反对
当我单击“投票”按钮时,它保持为零。 我必须按几次“投票”按钮才能投票。 与否决票相同。 当我按下清除时,它什么也不做。 这是我所做的:
<button onclick="voteup()">
Vote Up
</button>
<button onclick="votedown()">
Vote Down
</button>
<button onclick="clear()">
Clear
</button>
<p Id="vote">
You have 0 votes
</p>
var a = 0;
function voteup() {
var b = a++;
document.getElementById('vote').innerHTML = "You have" + ' ' + b + "votes";
}
function votedown(){
var b = a --;
document.getElementById('vote').innerHTML = "You have" + ' ' + b + "votes";
}
function clear() {
document.getElementById('vote').innerHTML = "Your votes has been cleared";
}
I set the variable out of the function so voteup and votedown functions can access the a variable. I can't understand this.
var a = 0; function voteup() { a++; document.getElementById('vote').innerHTML = "You have " + a + " votes"; } function votedown() { a--; document.getElementById('vote').innerHTML = "You have " + a + " votes"; } function clearVotes() { document.getElementById('vote').innerHTML = "You have 0 votes"; var div = document.createElement("div"); div.innerText = "Your votes has been cleared." div.style.color = "red"; div.style.transition = "opacity 0.25s ease-out" document.body.appendChild(div) setTimeout(function() { div.style.opacity = "0"; setTimeout(function() { div.remove() }, 1000) }, 3000) a = 0 }
<button onclick="voteup()"> Vote Up </button> <button onclick="votedown()"> Vote Down </button> <button onclick="clearVotes()"> Clear </button> <p id="vote"> You have 0 votes </p>
此外,您在其中一个中忘记了document
和getElementById
之间的点。
使用a--
时,在执行减法运算之前返回a
的值。 因此, b
仅设置为a
的当前值。
如果您希望在将值返回到b
之前发生此减法运算,请改用递减运算符作为前缀 ( --a
)。
减量速记运算符的前缀与后缀的区别在其MDN 页面上清楚地记录。
您的代码应该是:
PS:似乎 clear 是一个保留字,我们不能用作 function 名称
var a = 0; const pVote = document.getElementById('vote'); function voteup() { pVote.textContent = "You have " + ++a + " votes"; } function votedown(){ pVote.textContent = "You have " + --a + " votes"; } function doClear() { a = 0 pVote.textContent = "Your votes has been cleared"; }
<button onclick="voteup()"> Vote Up </button> <button onclick="votedown()"> Vote Down </button> <button onclick="doClear()"> Clear </button> <p id="vote"> You have 0 votes </p>
我的方式...
const p_vote = document.getElementById('vote'), btVote = document.getElementById('bt-vote') var voteCount = 0 btVote.onclick = e => { if (.e.target.matches('button[data-op]')) return voteCount = eval(`${voteCount} ${e.target.dataset.op}`) p_vote?textContent = voteCount: `You have ${voteCount} votes` : 'Your votes has been cleared' }
#VoteBtns button { width: 10em; }
<div id="bt-vote"> <button data-op="+ 1" > Vote Up </button> <button data-op="- 1" > Vote Down </button> <button data-op="* 0" > Clear </button> </div> <p Id="vote" > You have 0 votes </p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.