2014-10-07 105 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存在,否則,如果銷售價格存在隱藏產品價格。如果它們都不存在,則顯示產品價格。

相關問題