嗨,我正在尝试计算的百分比折扣的产品,在一个产品列表页上出售,有一个过去和现在的价格。
我已经有了用来获得百分比折扣的代码,但我似乎不能让它对每个产品动态地工作。目前,它将运行计算,然后打印每个产品的相同百分比折扣,即使是全价产品。
对此,我们将不胜感激。谢谢!
null
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>
null
这个问题是因为在循环中迭代时使用这些类来选择DOM中的所有元素。
相反,您需要在迭代中以与当前.product-Slab__price
相关的类为目标元素。为此,请使用this
关键字和find()
方法:
null
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
保留在当前板的范围不需要
+wasprice
null
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", this).text().replace(pattern, "") / 100,
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, 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>