javascript - How to increase, decrease and reset the counter in dynamic web application
问题描述
Here we have counter application to increase, decrease and reset the counter by using HTML, CSS, and JavaScript, but I can't getting to do how we can increase the counter and decrease the counter and reset the counter.
When the HTML button element with the id
decreaseBtn
is clicked, If the count is odd then decrease the counter value by -1 as shown in the image. If the count is even then decrease the counter value by -2 as shown in the image.When the HTML button element with the id increaseBtn is clicked, If the count is odd then increase the counter value by 10 as shown in the image. If the count is even then increase the counter value by 5 as shown in the image.
When the HTML button element with the id resetBtn is clicked, Set counter value as 0.
The Output image is Counter app
let counterElement = document.getElementById("counterValue");
function onIncrement() {
let previousCounterValue = counterElement.textContent;
let updatedCounterValue = parseInt(previousCounterValue) + 1;
if (updatedCounterValue > 0) {
counterElement.style.color = "black";
}
else if (updatedCounterValue < 0) {
counterElement.style.color = "black";
}
else {
counterElement.style.color = "black";
}
counterElement.textContent = updatedCounterValue;
}
function onDecrement() {
let previousCounterValue = counterElement.textContent;
let updatedCounterValue = parseInt(previousCounterValue) - 1;
if (updatedCounterValue > 0) {
counterElement.style.color = "black";
}
else if (updatedCounterValue < 0) {
counterElement.style.color = "black";
}
else {
counterElement.style.color = "black";
}
counterElement.textContent = updatedCounterValue;
}
function onReset() {
let counterValue = 0;
counterElement.textContent = counterValue;
counterElement.style.color = "black";
}
@import url("https://fonts.googleapis.com/css2?family=Bree+Serif&family=Caveat:wght@400;700&family=Lobster&family=Monoton&family=Open+Sans:ital,wght@0,400;0,700;1,400;1,700&family=Playfair+Display+SC:ital,wght@0,400;0,700;1,700&family=Playfair+Display:ital,wght@0,400;0,700;1,700&family=Roboto:ital,wght@0,400;0,700;1,400;1,700&family=Source+Sans+Pro:ital,wght@0,400;0,700;1,700&family=Work+Sans:ital,wght@0,400;0,700;1,700&display=swap");
.counter-value {
font-size: 36px;
font-weight: 900;
}
.button {
color: #ffffff;
background-color: #0967d2;
font-size: 14px;
border-width: 0;
border-radius: 4px;
padding: 10px;
}
<!DOCTYPE html>
<html>
<head> </head>
<body>
<p id="counterValue" class="counter-value">0</p>
<button id="decreaseBtn" class="button" onclick="onDecrement()">DECREASE</button>
<button id="resetBtn" class="button" onclick="onReset()">RESET</button>
<button id="increaseBtn" class="button" onclick="onIncrement()">INCREASE</button>
</body>
</html>
Expected Output is Counter app
解决方案
The other users in the comments have explained about how to check whether the count is odd or even.
I wanted to add an answer that approaches the code from a slightly different angle, and which I often find very useful.
Instead of picking up the textContent
of the counter element and parsing it to a number we maintain one number variable.
We give each button a data-type attribute, and a button container one handler function that is called immeditately.
The reason for this is so we can use a technique called a closure - a function returned from another function but maintains references to the variables that were declared before it was returned.
We can intitialise
count
.We return the closure.
When any button is clicked the event bubbles up the DOM and is captured by the listener in the container (this is called event delegation.
We check the data type. We update
count
based on the type and whether the count is odd or even.Finally we update the counter element text.
// Cache the elements
const counterElement = document.querySelector('#counterValue');
const container = document.querySelector('#container');
// Add a listener to the container
container.addEventListener('click', handleClick(), false);
// Small function to check whether the
// count is odd or even
function isEven(count) {
return count % 2 === 0;
}
// Initialise the count variable
function handleClick(count = 0) {
// Return a new function (the closure)
// as the function that will be called when
// the buttons are clicked
return function(e) {
// Get the button type from the clicked button
const { type } = e.target.dataset;
// Now just use `switch` to update the count
switch(type) {
case 'decrease': {
if (isEven(count)) {
count = count - 2;
} else {
count = count - 1;
}
break;
}
case 'increase': {
if (isEven(count)) {
count = count + 5;
} else {
count = count + 10;
}
break;
}
default:
case 'reset': count = 0; break;
}
// Finally update the counter element
counterElement.textContent = count;
}
}
.counter-value {font-size: 36px; font-weight: 900; }
.button {color: #ffffff; background-color: #0967d2; font-size: 14px; border-width: 0; border-radius: 4px; padding: 10px;}
<p id="counterValue" class="counter-value">0</p>
<div id="container">
<button data-type="decrease" class="button">DECREASE</button>
<button data-type="reset" class="button">RESET</button>
<button data-type="increase" class="button">INCREASE</button>
</div>
推荐阅读
- grails - Grails spring security userDetailService 导入服务
- python - 基于值的python dict键顺序 - 递归解决方案
- python - 将特定内容从一个 word doc 复制到另一个
- python-3.x - tkcalendar:更改 DateEntry() 的输出配置
- jenkins - Jenkins groovy:变量在管道中添加新行
- python - 无法从在 python 3 django 中使用 zmq 的函数返回
- python - Python 脚本将 ubuntu 终端命令发送到列表
- angular - 角度自定义输入组件未更新
- python - 如何在 Python 中模拟 cx_Oracle 游标
- python - Python 文件指针