如何解决计算产品列表页面上多个产品的百分比折扣
您好,我正在尝试计算产品列表页面上销售的产品的折扣百分比,这些产品具有过去和现在的价格。
我已经使用代码来获得百分比折扣,但我似乎无法让它为每个产品动态工作。目前,它会运行计算,然后为每个产品打印相同百分比的折扣,甚至是全价产品。
对此的任何帮助将不胜感激。谢谢!
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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。