写一个购物车加减的按钮
时间: 2023-05-11 21:06:19 浏览: 149
好的,以下是一个简单的购物车加减按钮的代码示例:
HTML代码:
```
<div class="cart">
<button class="minus">-</button>
<input type="text" class="quantity" value="1">
<button class="plus">+</button>
</div>
```
JavaScript代码:
```
var minusButton = document.querySelector(".cart .minus");
var plusButton = document.querySelector(".cart .plus");
var quantityInput = document.querySelector(".cart .quantity");
minusButton.addEventListener("click", function() {
var currentValue = parseInt(quantityInput.value);
if (currentValue > 1) {
quantityInput.value = currentValue - 1;
}
});
plusButton.addEventListener("click", function() {
var currentValue = parseInt(quantityInput.value);
quantityInput.value = currentValue + 1;
});
```
这段代码会在页面上创建一个购物车加减按钮,用户可以通过点击“+”和“-”按钮来增加或减少购物车中商品的数量。