我试图循环遍历所有价格跨度,然后插入一些显示其储蓄的HTML。.next()在每个循环中未定义
只要我删除.each
功能和(this).next
,我可以得到这个工作在一个产品上就好了。
我在Firebug得到的错误是TypeError: jQuery(...).next(...).html(...) is undefined
jQuery('.price').each(function(){
var pricea = parseInt(jQuery(this).next(".count .amount:first").html().replace("£","")) ;
var priceb = parseInt(jQuery(this).next(".count .amount:last").html().replace("£",""));
var total = (pricea - priceb)/(pricea) * 100;
var totalfixed = total.toFixed();
jQuery(this).next('.saving').append(totalfixed);
console.log(totalfixed);
});
我的HTML:
<li class="product ">
<span class="price">
<del class="count"><span class="amount2">WAS</span><span class="amount">£35</span></del>
<ins class="count"><span class="amount2">NOW </span><span class="amount">£20</span><span style="clear:both" class="saving"> <br><br> YOU SAVE %</span></ins> </span>
</li>
<li class="product ">
<span class="price">
<del class="count"><span class="amount2">WAS</span><span class="amount">£35</span></del>
<ins class="count"><span class="amount2">NOW </span><span class="amount">£20</span><span style="clear:both" class="saving"> <br><br> YOU SAVE %</span></ins> </span>
</li>
您可以查看现场演示Here
当我把上述代码放在[this jsfiddle](http://jsfiddle.net/biinjo/R4rjx/)中时,我得到了一个不同的错误:未捕获TypeError:无法调用未定义的方法'replace'。 你使用的是什么jQuery版本? –