花了几个小时在这件事上,似乎什么也没做!
我有5个相关的产品在页面底部。目前,无论这些产品是否贴现,都有一个“曾经”的价格显示。以上是实际价格,很明显,如果产品不打折,价格就会出现相同,这是不酷的。
因此,我编写了一个脚本,检查“实际”价格是否与"was“价格相同,如果为true,只需删除"was”Div元素。效果很好。
现在,我遇到的问题是让这个规则适用于一个页面中同一个类的多个元素。由于某种原因,当我试图在".each“函数中传递一个变量时,它返回一个字符串,该字符串包含每个".each”元素的所有Div值,而不是仅返回其相对父元素的值。例如“£674£1999.99 GB674”而不是"£674“。
检查实际价格是否与过去的价格相同:
$('.floatleft').each(function() {
var wasprice = $('.was').text();
var ourprice = $('.now').text();
if (wasprice == ourprice) {
$('div.price-extras').remove();
$('.productdetails-view .product-price .PricesalesPrice').css('line-height','40px');
};
});代码示例:
<div class="vmproduct-featured">
<div class=" width20 floatleft">
<div class="spacer">
<a href="/store/powertools/tacx-i-magic-t1900-trainer-info" title="Tacx I-Magic T1900 Trainer" class="img-link">
<img src="/store/product/resized/123_150x150.jpg" alt="123" class="featuredProductImage" border="0">
</a>
<div class="clear"></div>
<a class="text-link" href="/store/powertools/tacx-i-magic-t1900-trainer-info">Tacx I-Magic T1900 Trainer</a>
<div class="clear"></div>
<div class="was">£674.00</div>
<div class="now">£674.00</div>
</div>
</div>
<div class=" width20 floatleft">
<div class="spacer">
<a href="/store/powertools/2012-cube-agree-gtc-di2-info" title="2012 Cube Agree GTC DI2" class="img-link">
<img src="/store/product/resized/12cubeagreegtcdi2_150x150.jpg" alt="12cubeagreegtcdi2" class="featuredProductImage" border="0">
</a>
<div class="clear"></div>
<a class="text-link" href="/store/powertools/2012-cube-agree-gtc-di2-info">2012 Cube Agree GTC DI2
</a>
<div class="clear"></div>
<div class="was">£1,999.99</div>
<div class="now">£2,499.99</div>
</div>
</div>
</div>所以基本上我在.each()上失败了,需要一些帮助,请<3
发布于 2012-07-01 01:17:16
这是因为$('.was').text();同时选择了was类的所有文本,请尝试如下:
$('.was').each(function() {
var wasprice = $(this).text();
var ourprice = $(this).siblings('.now').text(); // or "$(this).next('.now').text();"
if (wasprice == ourprice) {
$('div.price-extras').remove();
$('.productdetails-view .product-price .PricesalesPrice').css('line-height','40px');
}
});https://stackoverflow.com/questions/11278879
复制相似问题