简体   繁体   English

当我点击向上或向下按钮时,我怎样才能做到这一点,数字增加或减少

[英]How can I make it so when I click the up or down button, the number increases or decreases

I'm trying to make a vote feature so when someone clicks the up arrow, the number increases, and when someone clicks the down arrow, the number decreases.我正在尝试制作投票功能,因此当有人单击向上箭头时,数字会增加,而当有人单击向下箭头时,数字会减少。

Here's my html and css这是我的 html 和 css

 .upVote { transform: rotate(-90deg); -webkit-transform: rotate(-90deg); background-color: white; border: none; }.downVote { transform: rotate(90deg); -webkit-transform: rotate(90deg); background-color: white; border: none; padding-bottom: -50; }
 <p>how do you vote?</p> <button class="upVote">></button> <span>0</span> <button class="downVote">></button>

Explanation: I have a added a function to increase/decrease the vote count depending on the parameter passed.说明:我添加了一个 function 以根据传递的参数增加/减少投票计数。 That function is called from both up vote & down vote buttons (plus one vote from up vote button & minus one vote from down vote button). function 从赞成票和反对票按钮都被调用(加上赞成票按钮一票,反对票按钮减去一票)。 If you down want to have negative votes, you can add a check inside the function to make the vote 0 when the votes goes down to zero.如果您想要反对票,您可以在 function 中添加一个检查,以在票数降至零时使票数为0 Like this像这样

vote = vote < 0 ? 0 : vote;

Try this.尝试这个。

 var vote = 0; const voteFun =(val) => { vote = vote + val; vote = vote < 0? 0: vote; //If you want vote not to go below zero document.getElementById('voteDisplay').innerHTML = vote; }
 .upVote, .downVote{ cursor:pointer; background-color: white; border: none; }.downVote { transform: rotate(90deg); -webkit-transform: rotate(90deg); }.upVote { transform: rotate(-90deg); -webkit-transform: rotate(-90deg); padding-bottom: -50; } </style>
 <p>how do you vote?</p> <button onclick="voteFun(-1)" class="downVote">></button> <span id="voteDisplay">0</span> <button onclick="voteFun(1)" class="upVote">></button>

 <style>.upVote { transform: rotate(-90deg); -webkit-transform: rotate(-90deg); background-color: white; border: none; padding-bottom: -50; }.downVote { transform: rotate(90deg); -webkit-transform: rotate(90deg); background-color: white; border: none; } </style> <script> function upVoteFunction() { var current = parseInt(document.getElementById("vote").innerHTML); document.getElementById("vote").innerHTML = current + 1; } function downVoteFunction() { var current = parseInt(document.getElementById("vote").innerHTML); document.getElementById("vote").innerHTML = current>0? current - 1: 0; } </script> <p>how do you vote?</p> <button class="downVote" onClick="downVoteFunction()">></button> <span id="vote">0</span> <button class="upVote" onClick="upVoteFunction()">></button>

暂无
暂无

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

相关问题 当我单击按钮时,其内容如何将其向上推,而不是向下推 - How when I click the button, its contents push it up, not down 当我单击按钮时无法弄清楚如何在js中上下滚动50px - Can't figure out how to scroll down/up by 50px in js when i click a button 如何使HTML元素被JavaScript隐藏,并在单击按钮时显示? - How can I make HTML elements be hidden by JavaScript, and show up when I click a button? 我可以检测 javascript 中的变量是增加还是减少值吗? - Can I detect if a variable in javascript increases or decreases in value? 如何格式化CSS和Bootstrap,以便在页面宽度减小时仅包含某些元素? - How can I format my CSS and Bootstrap so that only certain elements wrap when page width decreases? 当我点击一个按钮时,我的图像会发生变化,我该如何做到这一点? - How do I make it so when I click a button my image changes? 如何创建一个空的HTML锚点,以便在单击它时页面不会“跳起来”? - How can I create an empty HTML anchor so the page doesn't “jump up” when I click it? 当计时器倒数至“ 0”时,如何自动“单击”按钮? (如何调用代码中的按钮?) - How can I automatically “click” a button when a timer counts down to “0”? (How do I call click on a button from code?) 我如何编码以便每次运行时单击 p5.js 中的按钮? - how can i code so that when i click on button in p5.js everytime it runs? 如何使 window.prompt 变量仅在单击按钮时弹出? - How do I make so the window.prompt variables only pop up when a button is clicked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM