私のHTMLは次のようになります:
<li class="price">
<em>29.00</em>
<span class="shipping">Shipping:12.00</span>
<span class="comments"></span>
</li>
<li class="price">
<em>12.00</em>
<span class="shipping">Shipping:4.00</span>
<span class="comments"></span>
</li>
このパターンは、ページ上で約 9 ~ 15 回繰り返されます。
2 つの数値 (最初の例では、29.00 と 12.00) を抽出し、<li>
それらを互いに加算し (29+12=41)、結果に 2 を掛けてから、その結果を示す新しい要素を追加します (後にそれぞれ<li>
)。
もちろん、それぞれ<li>
に独自の結果が必要です。
-------------------------------------------------- ------------------------------------------------------
ついにすべてが一緒に働くようになりました!
(dasfisch と Barmar に感謝します) :
$(document).ready(function() {
var finalPrice = 0;
jQuery('.price').each(function(i) {
var myProductPrice = parseFloat(jQuery(this).children('em').html());
var myShippingPriceString = jQuery(this).children('.shipping').html();
//find the numeric portion of the string (decimal number)
var re = new RegExp();
re.compile("([0-9]+\.[0-9]*)");
var myShippingPrice = parseFloat(myShippingPriceString.match(re)[0]);
finalPrice = ((myProductPrice+myShippingPrice)*2).toString(); //your maths
jQuery(this).append('<li> product price: ' + myProductPrice + '</li>');
jQuery(this).append('<li> shipping price: ' + myShippingPrice + '</li>');
jQuery(this).append('<li> ==============</li>');
jQuery(this).append('<li> final price: ' + finalPrice + '</li>'); //change this to whatever HTML you want; the append is the important part
jQuery(this).append('<br><br>');
});
});