【发布时间】:2020-05-17 18:27:24
【问题描述】:
screenshot of my example 所以我为购物车做了一个柜台。每次按下 + 时,计数器都会加一。但我也希望 p 标签在你按下 + 时改变。正如您在屏幕截图中看到的那样,产品的价格是 159,95 欧元,所以每次我按 + 时,产品的价格应该会上涨 159,95 欧元。 这是我的代码:
const allCounters = document.getElementsByClassName('qty');
let min = -1;
let result = document.getElementById('res');
let price = document.getElementById('prijs');
for (const someCounter of allCounters) {
const minusBtn = someCounter.children[0];
const counter = someCounter.children[1];
const plusBtn = someCounter.children[2];
minusBtn.addEventListener('click', () => {
if (counter.value <= 0){
console.log('disabled')
}else {
counter.value = parseInt(counter.value) - 1;
}
});
plusBtn.addEventListener('click', () => {
counter.value = parseInt(counter.value) + 1;
});
}
.qty{
align-self: center;
}
.qty .count {
color: var(--color-black);
display: inline-block;
vertical-align: top;
font-size: 25px;
font-weight: 700;
line-height: 30px;
padding: 0 2px;
min-width: 35px;
text-align: center;
}
.qty .plus {
cursor: pointer;
display: inline-block;
vertical-align: top;
color: lightgrey;
width: 30px;
height: 30px;
font: 30px/1 Arial, sans-serif;
text-align: center;
}
.qty .minus {
cursor: pointer;
display: inline-block;
vertical-align: top;
color: lightgray;
width: 30px;
height: 30px;
font: 30px/1 Arial, sans-serif;
text-align: center;
background-clip: padding-box;
}
div {
text-align: center;
}
.minus:hover {
color: var(--color-black);
}
.plus:hover {
color: var(--color-black);
}
/*Prevent text selection*/
span {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
}
input {
border: 0;
width: 2%;
}
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
input:disabled {
background-color: white;
}
<div class="qty">
<span class="minus">-</span>
<input class="count" type="number" name="qty" value="1" />
<span class="plus">+</span>
</div>
<div class="prijs-product">
<p id="prijs">€ 159,95</p>
</div>
【问题讨论】:
标签: javascript html css counter shopping-cart