首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Jquery -通过变量和IF语句使用.each()

Jquery -通过变量和IF语句使用.each()
EN

Stack Overflow用户
提问于 2012-07-01 01:10:15
回答 1查看 22.6K关注 0票数 1

花了几个小时在这件事上,似乎什么也没做!

我有5个相关的产品在页面底部。目前,无论这些产品是否贴现,都有一个“曾经”的价格显示。以上是实际价格,很明显,如果产品不打折,价格就会出现相同,这是不酷的。

因此,我编写了一个脚本,检查“实际”价格是否与"was“价格相同,如果为true,只需删除"was”Div元素。效果很好。

现在,我遇到的问题是让这个规则适用于一个页面中同一个类的多个元素。由于某种原因,当我试图在".each“函数中传递一个变量时,它返回一个字符串,该字符串包含每个".each”元素的所有Div值,而不是仅返回其相对父元素的值。例如“£674£1999.99 GB674”而不是"£674“。

检查实际价格是否与过去的价格相同:

代码语言:javascript
运行
复制
    $('.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');

};
});

代码示例:

代码语言:javascript
运行
复制
<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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-01 01:17:16

这是因为$('.was').text();同时选择了was类的所有文本,请尝试如下:

代码语言:javascript
运行
复制
$('.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');
    }
});
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11278879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档