我正在尝试将相同的数学更改应用于 6 个不同跨度中的 6 个不同数字,它们都共享同一类。使用此 HTML:
<span class="PageText_L483n">$8.00</span>
<span class="PageText_L483n">$9.00</span>
<span class="PageText_L483n">$10.00</span>
<span class="PageText_L483n">$11.00</span>
<span class="PageText_L483n">$12.00</span>
<span class="PageText_L483n">$13.00</span>
我最初有这个JS:
$(function() {
var price = parseFloat($('.PageText_L483n').text().substr(1));
var discount = price * 0.2;
var newPrice = price - discount;
var newText = '<div>$' + price + '</div> $' + newPrice;
$('.PageText_L483n').html(newText);
$('.PageText_L483n div').css("text-decoration", "line-through");
});
但这只会用第一个跨度中的信息替换所有跨度。然后我尝试在这个 JS 中使用数组:
$(function() {
var prices = [];
for (var i = 0; i < 6; i++) {
prices[i] = parseFloat($('.PageText_L483n:nth-of-type(i+1)').text().trim().substr(1));
}
for (var j = 0; j < 6; j++) {
var discount = prices[j] * 0.2;
var newPrice = prices[j] - discount;
var newText = '<div>$' + prices[j] + '</div> $' + newPrice;
$('.PageText_L483n').html(newText);
$('.PageText_L483n div').css("text-decoration", "line-through");
}
});
但现在它什么也没做。有人能指出我正确的方向吗?
JSFiddle:http: //jsfiddle.net/vSePd/