问题描述
您好,我正在尝试计算产品列表页面上销售的产品的折扣百分比,这些产品具有过去和现在的价格。
我已经使用代码来获得百分比折扣,但我似乎无法让它为每个产品动态工作。目前,它会运行计算,然后为每个产品打印相同百分比的折扣,甚至是全价产品。
对此的任何帮助将不胜感激。谢谢!
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) + "%");
});
},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 €10,00</span>
<span class="product-price__value product-price__value--discounted">Now €8,00</span>
<span class="discountPercentage" > </span>
</div>
</div>
<div class="product-slab__price">
<div class="product-price__primary">
<span class="product-price__was">€15,00</span>
<span class="product-price__value product-price__value--discounted">€10,00</span>
<span class="discountPercentage"></span>
</div>
</div>
<div class="product-slab__price">
<div class="product-price__primary">
<span class="product-price__value">€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) + "%");
});
},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 €10,00</span>
<span class="product-price__value product-price__value--discounted">Now €8,00</span>
<span class="discountPercentage"> </span>
</div>
</div>
<div class="product-slab__price">
<div class="product-price__primary">
<span class="product-price__was">€15,00</span>
<span class="product-price__value product-price__value--discounted">€10,00</span>
<span class="discountPercentage"></span>
</div>
</div>
<div class="product-slab__price">
<div class="product-price__primary">
<span class="product-price__value">€15,90</span>
<span class="discountPercentage"></span>
</div>
</div>
请注意,由于正则表达式内容是静态的,因此可以在循环外定义。
,你需要
- 使用
,this
保持在当前slab的范围内 - 测试乘法价格是一个数字
- 移动正则表达式,因为您不需要每次都创建它
不需要
- 通过您显示的代码中的 .product-price__primary
- parseFloat 因为你没有在任何地方添加。如果您确实需要添加 ,可以使用一元加号
+wasPrice
const pattern = /[^0-9\.]/g,discountPrice = function() {
$('.product-slab__price').each(function() {
const wasPrice = $(".product-price__was",this).text().replace(pattern,"") / 100,newPrice = $(".product-price__value--discounted",subtractPrice = Math.abs(wasPrice - newPrice),dividePrice = Math.abs(subtractPrice / wasPrice),multiplyPrice = Math.abs(dividePrice * 100);
if (!isNaN(multiplyPrice)) $('.discountPercentage',this).html(multiplyPrice.toFixed(0) + "%");
});
};
setTimeout(discountPrice,90</span>
<span class="discountPercentage"></span>
</div>
</div>