2014-10-07 107 views
1

我有一个电子商务网站有一个给定的项目的多个价格。如果价格较低的类出现在页面上,我正尝试设置一些隐藏价格的脚本。如果元素ID存在,那么隐藏其他元素

<table> 
    <tbody> 
     <tr> 
      <td> 
       <font> 
        <div class="product_listprice">199.99</div> 
       </font> 
       <font> 
        <div class="product_productprice">179.99</div> 
       </font> 
       <b> 
        <div class="product_saleprice">159.99</div> 
       </b> 
       <font> 
        <div class="product_discountprice">139.99</div> 
       </font> 
      </td> 
     </tr> 
    </tbody> 
</table> 

基本上我需要的是一个脚本,如果.product_saleprice存在的页面上,如果存在.product_discountprice将隐藏既.product_saleprice和.product_productprice,将隐藏.product_productprice。

以下是我已经拿出一些谷歌挖掘到现在。

<script type="text/javascript"> 
    $(document).ready(function(){ 
     if ($(".product_discountprice").size()) 
     $(".product_saleprice, .product_productprice").hide(); 
     }); 
    }); 
</script> 

但是,它似乎并没有工作。有任何想法吗?我是一个jQuery新手,所以我敢肯定有更好的方式来做到这一点有...

+4

''? 20世纪90年代呼吁并希望它的html回来。 – 2014-10-07 19:17:30

+0

请告诉我,请将您的疑虑和意见发送到www.volusion.com – 2014-10-07 19:18:23

+0

@MarcB您会意识到目前最终版本的HTML(4)最初是在20世纪90年代发布的:) – mattytommo 2014-10-07 19:19:26

回答

3
// Essentially what I need is a script that will hide .product_productprice 
// if .product_saleprice exists on the page... 
if ($('.product_saleprice').length) { 
    $('.product_productprice').hide(); 
} 

// ...and will hide both .product_saleprice and .product_productprice 
// if .product_discountprice exists. 
if ($('.product_discountprice').length) { 
    $('.product_saleprice, .product_productprice').hide(); 
} 

更新,增加了一个新的类名称,而不是隐藏:

if ($('.product_saleprice').length) { 
    $('.product_productprice').addClass('some-class'); 
} 

if ($('.product_discountprice').length) { 
    $('.product_saleprice, .product_productprice').addClass('some-class'); 
} 
+0

'.size()'与'.length'相同。 – dfsq 2014-10-07 19:20:28

+0

除了['.size()'被弃用为'.length'](http://api.jquery.com/size/)之外。 – jmar777 2014-10-07 19:21:04

+0

是的,这固定了我已经想出来的一点。但是,只有折扣价存在时才会隐藏。 – 2014-10-07 19:21:08

-1

试试这个..不确定它是否正确。

<script type="text/javascript"> 
     $(document).ready(function(){ 
      if($(".product_discountprice").length()){ 
       $('.product_saleprice').hide(); 
       $('.product_productprice').hide(); 
      }); 
     }); 
    </script> 
+0

我相信这是不正确的 – hakazvaka 2014-10-07 19:22:19

0

http://jsfiddle.net/3481uqa4/1/

if ($(".product_discountprice").length) { 
     $(".product_productprice, .product_saleprice").hide();  
    } else if ($(".product_saleprice").length) { 
     $(".product_productprice").hide(); 
    } 

将隐藏无论是产品的价格和销售价格,如果product_discountprice存在,否则,如果销售价格存在隐藏产品价格。如果它们都不存在,则显示产品价格。

相关问题