2013-11-15 73 views
0

我正在开发一家咖啡店产品网站。我们有一个区域,每个产品都有一个咖啡强度指示器。数据库根据强度是强,中,弱还是不适用来生成一个值。不适用于非咖啡产品。如果显示n/a值,隐藏DIV

如果显示n/a,我想隐藏包含div。

我到目前为止的代码如下。我有一些JavaScript代替了数据库显示的文本和强度指标的图片。

如果span标签中的咖啡强度不是我想隐藏的。

这可能吗?

在此先感谢。

<div class="coffee-strength"> 
      <p>Coffee Strength: <span><?php echo $_product->getAttributeText('coffeestrength'); ?></span></p> 
</div> 



<script type="text/javascript"> 

      jQuery(function ($) { 
       $('.coffee-strength p span').each(function() { 
        var string = $.trim($(this).text()); 
        $(this).html('<img src="/images/' + string + '.png" alt="' + string + '" />'); 
       }); 

      }); 

</script> 
+1

也许你可以添加这个'<?PHP的echo $ _product-> getAttributeText( 'coffeestrength');在div的类中,就像JS中的那样,你可以检查这个类来隐藏它。 – Franck

回答

0
  jQuery(function ($) { 
      $('.coffee-strength p span').each(function() { 
       var string = $.trim($(this).text()); 
       if(string!="n/a"){ 
        $(this).html('<img src="/images/' + string + '.png" alt="' + string + '" />'); 
       }else{ 
        $(this).hide();  
       } 
       }); 

     }); 
2

这应该工作:

jQuery(function ($) { 
    $('.coffee-strength p span').each(function() { 
     var string = $.trim($(this).text()); 

     if (string == "n/a") 
      $(this).closest('.coffee-strength').hide(); 
     else 
      $(this).html('<img src="/images/' + string + '.png" alt="' + string + '" />'); 
    }); 
}); 
+0

我会推荐更可读的'.closest('。coffee-strength')'而不是'.parent()。parent()' – Tomas

+0

@Tomas很好。刚更新了答案。 – melancia

0

更新后的脚本:

<script type="text/javascript"> 

      jQuery(function ($) { 
       $('.coffee-strength p span').each(function() { 
        var string = $.trim($(this).text()); 
        if(string!="22"){ 
         $(this).html('<img src="/images/' + string + '.png" alt="' + string + '" />'); 
        }else{ 
         $(this).closest('.coffee-strength').hide();  
        } 
        }); 

      }); 

      </script> 
0

你有很多方法可以这样:

HTML代码:

<div class="coffee-strength"> 
    <p>Coffee Strength: <span>Strong</span></p> 
</div> 
<div class="coffee-strength"> 
    <p>Coffee Strength: <span>Dim</span></p> 
</div> 
<div class="coffee-strength"> 
    <p>Coffee Strength: <span>n/a</span></p> 
</div> 

jQuery代码:

$(function ($) { 
    $('.coffee-strength p span').each(function() { 
     var string = $.trim($(this).text()); 
     if (string == 'n/a') { 
      $(this).parent().hide(); 
     } 
    }); 
}); 

// or 

$(function ($) { 
    $('.coffee-strength p span:contains("n/a")').parent().hide(); 
});