Jquery - 使用.each()和变量以及IF语句

Ale*_*xon 1 variables each jquery if-statement

现在花了好几个小时,似乎没有到达任何地方!

我在页面底部有5个相关产品.目前,无论是否打折,这些产品都会显示"是"价格.位于上方的是实际价格,显然如果产品没有打折,价格会出现相同,这并不酷.

所以我写了一个脚本,检查"实际"价格是否与"是"价格相同,如果为真,它只是删除"是"Div元素.工作良好.

现在我遇到的问题是使这个规则适用于多个元素与页面中的同一个类.出于某种原因,当我尝试在".each"函数中传递变量时,它返回一个字符串,其中包含每个".each"元素的所有Div值,而不是仅返回其相对父元素的值.例如"£674£1999.99£674"而不是"£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');

};
});
Run Code Online (Sandbox Code Playgroud)

HTML代码示例:

<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>
Run Code Online (Sandbox Code Playgroud)

所以基本上我在.each()失败并需要一些帮助,请<3

und*_*ned 5

那是因为一次$('.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');
    }
});
Run Code Online (Sandbox Code Playgroud)