我试图通过JQuery修改价格。当我将代码添加到控制台时似乎都没问题,但html没有改变。此外,当我调用console.log(newPrc)
时,它说“newPrc”未定义。我的代码是:
(function($) {
Number.prototype.formatMoney = function(c, d, t) {
var n = this,
c = isNaN(c = Math.abs(c)) ? 2 : c,
d = d == undefined ? "." : d,
t = t == undefined ? "," : t,
s = n < 0 ? "-" : "",
i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
j = (j = i.length) > 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};
$(document).ready(function() {
oldPrc = jQuery('body.checkout-delivery .basket__totals #grand-total-price .price').html();
newPrc = oldPrc.replace('<span class="currency">£</span>', '');
newPrc = newPrc.split(',');
newPrc = newPrc[0] + newPrc[1];
newPrc = parseInt(newPrc) + 39;
newPrc = '<span class="currency mtI">£</span>' + newPrc;
jQuery('body.checkout-delivery .basket__totals #grand-total-price .price:has(.currency:not(.mtI))').html(newPrc);
});
})(jQuery);
答案 0 :(得分:1)
一旦我将jQuery和相应的演示标记添加到代码段,它就会显示为原始价格添加39(实际上不是39加上4.99不是43)并在HTML中显示结果:
(function($) {
Number.prototype.formatMoney = function(c, d, t) {
var n = this,
c = isNaN(c = Math.abs(c)) ? 2 : c,
d = d == undefined ? "." : d,
t = t == undefined ? "," : t,
s = n < 0 ? "-" : "",
i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
j = (j = i.length) > 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};
$(document).ready(function() {
oldPrc = jQuery('body.checkout-delivery .basket__totals #grand-total-price .price').html();
newPrc = oldPrc.replace('<span class="currency">£</span>', '');
newPrc = newPrc.split(',');
newPrc = newPrc[0] + newPrc[1];
newPrc = parseInt(newPrc) + 39;
newPrc = '<span class="currency mtI">£</span>' + newPrc;
jQuery('body.checkout-delivery .basket__totals #grand-total-price .price:has(.currency:not(.mtI))').html(newPrc);
});
})(jQuery);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body class="checkout-delivery">
<div class="basket__totals">
<div id="grand-total-price">
<div class="price"><span class="currency">£</span>4.99</div>
</div>
</div>
</body>
&#13;
您的标记看起来像上面的标记吗?