gpt4 book ai didi

javascript - 我怎样才能让这个javascript不止一次地工作?

转载 作者:行者123 更新时间:2023-11-28 16:19:55 26 4
gpt4 key购买 nike

我在使用以下 javascript 时遇到问题。问题出在“+”和“-”符号上。它对左边的第一个盒子工作得很好,但我不能让它与中间或最右边的盒子一起工作。我希望中间和右边的盒子像左边的第一个盒子一样工作,所以当我按下“+”时,数量会增加,如果我按下“-”,数量会减少。谢谢!

function incrementQty() {
var value = document.querySelector('input[name="qty"]').value;
var cardQty = document.querySelector(".cart-qty");
value = isNaN(value) ? 1 : value;
value++;
document.querySelector('input[name="qty"]').value = value;
cardQty.innerHTML = value;
cardQty.classList.add("rotate-x");
}

function decrementQty() {
var value = document.querySelector('input[name="qty"]').value;
var cardQty = document.querySelector(".cart-qty");
value = isNaN(value) ? 1 : value;
value > 1 ? value-- : value;
document.querySelector('input[name="qty"]').value = value;
cardQty.innerHTML = value;
cardQty.classList.add("rotate-x");
}

function removeAnimation(e) {
e.target.classList.remove("rotate-x");
}

const counter = document.querySelector(".cart-qty");
counter.addEventListener("animationend", removeAnimation);
$text-color: #2a2a2a;
$bg-color: #f2eee9;
*,
*:before,
*:after {
box-sizing: border-box;
}

@mixin clearfix {
content: '';
display: table;
clear: both;
}

body {
font-family: 'Open Sans', sans-serif;
color: $text-color;
display: flex;
height: 100vh;
align-items: center;
font-size: 14px;
background: $bg-color;
background-size: cover;
}

.container {
position: relative;
width: 100%;
max-width: 280px;
margin: 0 auto;
&:after {
@include clearfix;
}
}

h1 {
display: inline-block;
background-color: $text-color;
color: #fff;
font-size: 20px;
font-weight: normal;
text-transform: uppercase;
padding: 4px 20px;
float: left;
}

.item {
background-color: #fff;
padding: 10px;
box-shadow: 0 1.5px 4px rgba(0, 0, 0, 0.24), 0 1.5px 6px rgba(0, 0, 0, 0.12);
border: 1px solid #FFFEFD;
img {
display: block;
margin: auto;
padding: 20px;
width: 180px;
}
h2 {
font-size: 1.1em;
font-weight: normal;
display: block;
border-bottom: 1px solid #ccc;
margin: 10px 0 10px 0;
padding: 0 0 5px 0;
em {
display: block;
line-height: 1.6;
font-size: .8em;
}
}
}

.cart-button {
float: right;
margin: 12px 15px 0 0;
img {
width: 30px;
height: 30px;
margin: 0 auto;
display: block;
color: #888;
}
.cart-qty {
background: #ff5252;
border-radius: 50%;
color: #fff;
display: block;
font-size: .8em;
line-height: 17px;
position: absolute;
text-align: center;
top: 11px;
right: 10px;
height: 17px;
width: 17px;
}
}

.qty-block {
margin-top: 20px;
}

.qty {
float: left;
width: 80px;
margin-right: 10px;
user-select: none;
}

.increment,
.decrement {
.qty_inc_dec & {
float: left;
text-align: center;
width: 30px;
cursor: pointer;
font-size: 1.2em;
line-height: 20px;
height: 25px;
vertical-align: middle;
background-color: #fff;
border: 1px solid #ccc;
}
}

.increment {
.qty_inc_dec & {
border-bottom: 0;
line-height: 25px;
}
}

.qty_inc_dec {
float: left;
width: 10px;
height: 50px;
display: inline-block;
}

input[type="text"] {
.qty & {
float: left;
font-family: "Open Sans", sans-serif;
outline: 0;
font-size: 1.2em;
text-align: center;
width: 50px;
height: 50px;
color: $text-color;
line-height: 40px;
border: 1px solid #ccc;
border-right: 0;
}
}

button[type="button"] {
cursor: pointer;
width: 168px;
border: none;
color: $text-color;
background: #fff;
height: 50px;
font-size: 1.2em;
font-family: 'Open Sans', sans-serif;
transition: all .2s;
border: 1px solid #ccc;
box-shadow: 0 1px 2px #fff;
&:hover {
box-shadow: 0 1px 2px #cbc3ba;
}
&:active,
&:focus {
outline: none;
}
}

.rotate-x {
animation-duration: .6s;
animation-name: rotate-x;
}

@keyframes rotate-x {
from {
transform: rotateY(0deg);
}
to {
transform: rotateY(360deg);
}
}
<div class="container">
<h1>Holiday <strong>Deals</strong></h1>
<div class="cart-button">
<img src="http://www.milanmilosev.com/external/codepen/img/cart.png" width="30" height="30">
<span class="cart-qty">1</span> </div>
<div class="item">
<img src="http://www.milanmilosev.com/external/codepen/img/asus.png" alt="">
<h2>ASUS E200HA-UB02-GD<em>Intel Quad-Core 4GB RAM 32GB Storage</em></h2>
<p>Price: <em>$439.12</em></p>
<div class="qty-block">
<div class="qty">
<input type="text" name="qty" maxlength="12" value="1" title="" class="input-text" />
<div class="qty_inc_dec">
<i class="increment" onclick="incrementQty()">+</i>
<i class="decrement" onclick="decrementQty()">-</i>
</div>
</div>
<button type="button" title="Add to Cart" class="btn-cart">Add to Cart</button>
</div>
</div>
</div>

<div class="container">
<h1>Holiday <strong>Deals</strong></h1>
<div class="cart-button">
<img src="http://www.milanmilosev.com/external/codepen/img/cart.png" width="30" height="30">
<span class="cart-qty">1</span> </div>
<div class="item">
<img src="http://www.milanmilosev.com/external/codepen/img/asus.png" alt="">
<h2>ASUS E200HA-UB02-GD<em>Intel Quad-Core 4GB RAM 32GB Storage</em></h2>
<p>Price: <em>$439.12</em></p>
<div class="qty-block">
<div class="qty">
<input type="text" name="qty" maxlength="12" value="1" title="" class="input-text" />
<div class="qty_inc_dec">
<i class="increment" onclick="incrementQty()">+</i>
<i class="decrement" onclick="decrementQty()">-</i>
</div>
</div>
<button type="button" title="Add to Cart" class="btn-cart">Add to Cart</button>
</div>
</div>
</div>

<div class="container">
<h1>Holiday <strong>Deals</strong></h1>
<div class="cart-button">
<img src="http://www.milanmilosev.com/external/codepen/img/cart.png" width="30" height="30">
<span class="cart-qty">1</span> </div>
<div class="item">
<img src="http://www.milanmilosev.com/external/codepen/img/asus.png" alt="">
<h2>ASUS E200HA-UB02-GD<em>Intel Quad-Core 4GB RAM 32GB Storage</em></h2>
<p>Price: <em>$439.12</em></p>
<div class="qty-block">
<div class="qty">
<input type="text" name="qty" maxlength="12" value="1" title="" class="input-text" />
<div class="qty_inc_dec">
<i class="increment" onclick="incrementQty()">+</i>
<i class="decrement" onclick="decrementQty()">-</i>
</div>
</div>
<button type="button" title="Add to Cart" class="btn-cart">Add to Cart</button>
</div>
</div>
</div>

最佳答案

简短的回答是否定的。你不能用你当前的设置来做到这一点。为什么?您拥有的每个元素都应该有一个唯一的标识符。假设我们将在您的容器 div 中附加唯一标识符(因为这似乎是每个元素的父级)。

当然,我会以不同的方式执行此操作并添加一个 id,但出于演示目的,我使 html 结构最接近您的结构。

  1. 为您的容器 class="container-1"添加一个编号/id 作为类。在创建 dom 期间自动增加数字

  2. 在递增/递减函数中传递数字/id。这将有助于确定您指的是哪个控件。

  3. 更改查询选择器以在查询选择器子句中获取容器父级例如var cardQty = document.querySelector('.container-'+num+' .cart-qty');

好了。它现在正在工作。

function incrementQty(num) {
var targetQtyTextbox= '.container-'+num+' input[name="qty"]';
var value = document.querySelector(targetQtyTextbox).value;
var cardQty = document.querySelector('.container-'+num+' .cart-qty');
value = isNaN(value) ? 1 : value;
value++;
document.querySelector(targetQtyTextbox).value = value;
cardQty.innerHTML = value;
cardQty.classList.add("rotate-x");
}

function decrementQty(num) {
var targetQtyTextbox= '.container-'+num+' input[name="qty"]';
var value = document.querySelector(targetQtyTextbox).value;
var cardQty = document.querySelector('.container-'+num+' .cart-qty');
value = isNaN(value) ? 1 : value;
value > 1 ? value-- : value;
document.querySelector(targetQtyTextbox).value = value;
cardQty.innerHTML = value;
cardQty.classList.add("rotate-x");
}

function removeAnimation(e) {
e.target.classList.remove("rotate-x");
}

const counter = document.querySelector(".cart-qty");
counter.addEventListener("animationend", removeAnimation);
$text-color: #2a2a2a;
$bg-color: #f2eee9;
*,
*:before,
*:after {
box-sizing: border-box;
}

@mixin clearfix {
content: '';
display: table;
clear: both;
}

body {
font-family: 'Open Sans', sans-serif;
color: $text-color;
display: flex;
height: 100vh;
align-items: center;
font-size: 14px;
background: $bg-color;
background-size: cover;
}

.container {
position: relative;
width: 100%;
max-width: 280px;
margin: 0 auto;
&:after {
@include clearfix;
}
}

h1 {
display: inline-block;
background-color: $text-color;
color: #fff;
font-size: 20px;
font-weight: normal;
text-transform: uppercase;
padding: 4px 20px;
float: left;
}

.item {
background-color: #fff;
padding: 10px;
box-shadow: 0 1.5px 4px rgba(0, 0, 0, 0.24), 0 1.5px 6px rgba(0, 0, 0, 0.12);
border: 1px solid #FFFEFD;
img {
display: block;
margin: auto;
padding: 20px;
width: 180px;
}
h2 {
font-size: 1.1em;
font-weight: normal;
display: block;
border-bottom: 1px solid #ccc;
margin: 10px 0 10px 0;
padding: 0 0 5px 0;
em {
display: block;
line-height: 1.6;
font-size: .8em;
}
}
}

.cart-button {
float: right;
margin: 12px 15px 0 0;
img {
width: 30px;
height: 30px;
margin: 0 auto;
display: block;
color: #888;
}
.cart-qty {
background: #ff5252;
border-radius: 50%;
color: #fff;
display: block;
font-size: .8em;
line-height: 17px;
position: absolute;
text-align: center;
top: 11px;
right: 10px;
height: 17px;
width: 17px;
}
}

.qty-block {
margin-top: 20px;
}

.qty {
float: left;
width: 80px;
margin-right: 10px;
user-select: none;
}

.increment,
.decrement {
.qty_inc_dec & {
float: left;
text-align: center;
width: 30px;
cursor: pointer;
font-size: 1.2em;
line-height: 20px;
height: 25px;
vertical-align: middle;
background-color: #fff;
border: 1px solid #ccc;
}
}

.increment {
.qty_inc_dec & {
border-bottom: 0;
line-height: 25px;
}
}

.qty_inc_dec {
float: left;
width: 10px;
height: 50px;
display: inline-block;
}

input[type="text"] {
.qty & {
float: left;
font-family: "Open Sans", sans-serif;
outline: 0;
font-size: 1.2em;
text-align: center;
width: 50px;
height: 50px;
color: $text-color;
line-height: 40px;
border: 1px solid #ccc;
border-right: 0;
}
}

button[type="button"] {
cursor: pointer;
width: 168px;
border: none;
color: $text-color;
background: #fff;
height: 50px;
font-size: 1.2em;
font-family: 'Open Sans', sans-serif;
transition: all .2s;
border: 1px solid #ccc;
box-shadow: 0 1px 2px #fff;
&:hover {
box-shadow: 0 1px 2px #cbc3ba;
}
&:active,
&:focus {
outline: none;
}
}

.rotate-x {
animation-duration: .6s;
animation-name: rotate-x;
}

@keyframes rotate-x {
from {
transform: rotateY(0deg);
}
to {
transform: rotateY(360deg);
}
}
<div class="container-1">
<h1>Holiday <strong>Deals</strong></h1>
<div class="cart-button">
<img src="http://www.milanmilosev.com/external/codepen/img/cart.png" width="30" height="30">
<span class="cart-qty">1</span> </div>
<div class="item">
<img src="http://www.milanmilosev.com/external/codepen/img/asus.png" alt="">
<h2>ASUS E200HA-UB02-GD<em>Intel Quad-Core 4GB RAM 32GB Storage</em></h2>
<p>Price: <em>$439.12</em></p>
<div class="qty-block">
<div class="qty">
<input type="text" name="qty" maxlength="12" value="1" title="" class="input-text" />
<div class="qty_inc_dec">
<i class="increment" onclick="incrementQty(1)">+</i>
<i class="decrement" onclick="decrementQty(1)">-</i>
</div>
</div>
<button type="button" title="Add to Cart" class="btn-cart">Add to Cart</button>
</div>
</div>
</div>

<div class="container-2">
<h1>Holiday <strong>Deals</strong></h1>
<div class="cart-button">
<img src="http://www.milanmilosev.com/external/codepen/img/cart.png" width="30" height="30">
<span class="cart-qty">1</span> </div>
<div class="item">
<img src="http://www.milanmilosev.com/external/codepen/img/asus.png" alt="">
<h2>ASUS E200HA-UB02-GD<em>Intel Quad-Core 4GB RAM 32GB Storage</em></h2>
<p>Price: <em>$439.12</em></p>
<div class="qty-block">
<div class="qty">
<input type="text" name="qty" maxlength="12" value="1" title="" class="input-text" />
<div class="qty_inc_dec">
<i class="increment" onclick="incrementQty(2)">+</i>
<i class="decrement" onclick="decrementQty(2)">-</i>
</div>
</div>
<button type="button" title="Add to Cart" class="btn-cart">Add to Cart</button>
</div>
</div>
</div>

<div class="container-3">
<h1>Holiday <strong>Deals</strong></h1>
<div class="cart-button">
<img src="http://www.milanmilosev.com/external/codepen/img/cart.png" width="30" height="30">
<span class="cart-qty">1</span> </div>
<div class="item">
<img src="http://www.milanmilosev.com/external/codepen/img/asus.png" alt="">
<h2>ASUS E200HA-UB02-GD<em>Intel Quad-Core 4GB RAM 32GB Storage</em></h2>
<p>Price: <em>$439.12</em></p>
<div class="qty-block">
<div class="qty">
<input type="text" name="qty" maxlength="12" value="1" title="" class="input-text" />
<div class="qty_inc_dec">
<i class="increment" onclick="incrementQty(3)">+</i>
<i class="decrement" onclick="decrementQty(3)">-</i>
</div>
</div>
<button type="button" title="Add to Cart" class="btn-cart">Add to Cart</button>
</div>
</div>
</div>

关于javascript - 我怎样才能让这个javascript不止一次地工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53531160/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com