gpt4 book ai didi

jquery - 计算产品列表页面上多个产品的百分比折扣

转载 作者:行者123 更新时间:2023-12-04 07:59:50 25 4
gpt4 key购买 nike

嗨,我正在尝试计算产品列表页面上销售的产品的折扣百分比,这些产品具有过去和现在的价格。
我已经使用代码来获得百分比折扣,但我似乎无法让它为每个产品动态工作。目前,它会运行计算,然后为每个产品打印相同的折扣百分比,甚至是全价产品。
对此的任何帮助将不胜感激。谢谢!

setTimeout(function discountPrice() {
$('.product-slab__price').each(function() {
var pattern = /[^0-9\.]/g;
var wasPrice = parseFloat($(".product-price__primary .product-price__was").text().replace(pattern, "")) / 100;
var newPrice = parseFloat($(".product-price__primary .product-price__value--discounted").text().replace(pattern, "") / 100);
var subtractPrice = (Math.abs(wasPrice - newPrice));
var dividePrice = (Math.abs(subtractPrice / wasPrice));
var multiplyPrice = (Math.abs(dividePrice * 100));
$('.discountPercentage').html(multiplyPrice.toFixed(0) + "&#37");
});
}, 500);
.product-price__was {
text-decoration: line-through;
}

.product-price__value.product-price__value--discounted {
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="product-slab__price">
<div class="product-price__primary">
<span class="product-price__was">Was &euro;10,00</span>
<span class="product-price__value product-price__value--discounted">Now &euro;8,00</span>
<span class="discountPercentage" > </span>
</div>
</div>

<div class="product-slab__price">
<div class="product-price__primary">
<span class="product-price__was">&euro;15,00</span>
<span class="product-price__value product-price__value--discounted">&euro;10,00</span>
<span class="discountPercentage"></span>
</div>
</div>


<div class="product-slab__price">
<div class="product-price__primary">
<span class="product-price__value">&euro;15,90</span>
<span class="discountPercentage"></span>
</div>
</div>

最佳答案

问题是因为您在循环中使用这些类选择 DOM 中的所有元素。
相反,您需要使用与当前 .product-slab__price 相关的那些类来定位元素。在迭代中。要做到这一点,请使用 this关键字和 find()方法:

setTimeout(function discountPrice() {
var pattern = /[^0-9\.]/g;

$('.product-slab__price:has(.product-price__value--discounted)').each(function() {
let $slab = $(this);
var wasPrice = parseFloat($slab.find(".product-price__primary .product-price__was").text().replace(pattern, "")) / 100;
var newPrice = parseFloat($slab.find(".product-price__primary .product-price__value--discounted").text().replace(pattern, "") / 100);

var subtractPrice = Math.abs(wasPrice - newPrice);
var dividePrice = Math.abs(subtractPrice / wasPrice);
var multiplyPrice = Math.abs(dividePrice * 100);

$slab.find('.discountPercentage').html(multiplyPrice.toFixed(0) + "&#37");
});
}, 500);
.product-price__was {
text-decoration: line-through;
}

.product-price__value.product-price__value--discounted {
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="product-slab__price">
<div class="product-price__primary">
<span class="product-price__was">Was &euro;10,00</span>
<span class="product-price__value product-price__value--discounted">Now &euro;8,00</span>
<span class="discountPercentage"> </span>
</div>
</div>

<div class="product-slab__price">
<div class="product-price__primary">
<span class="product-price__was">&euro;15,00</span>
<span class="product-price__value product-price__value--discounted">&euro;10,00</span>
<span class="discountPercentage"></span>
</div>
</div>


<div class="product-slab__price">
<div class="product-price__primary">
<span class="product-price__value">&euro;15,90</span>
<span class="discountPercentage"></span>
</div>
</div>

请注意,由于正则表达式内容是静态的,因此可以在循环外定义。

关于jquery - 计算产品列表页面上多个产品的百分比折扣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66530883/

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