[英]Toggle class upon change of data attribute
我正在使用購物車,我的客戶希望它在空時有一個背景,在有物品時又有一個背景。 所以這是我得到的標記:
<a href="#" id="shopping-cart" data-count="0" class="trigger cart">
<span>
</span>
</a>
數據計數的更改正在服務器端處理並通過php更新我的問題是我不知道如何讓jquery讀取數據計數並制作if語句,以便它讀取
if data-count= 0 then add .empty
else data-count > 0 then add .full
另外,我不確定是否應該使用.addClass
或.toggleClass
從空到滿,很抱歉,到目前為止,我所有編寫jquery的嘗試都失敗了,並且所有正在處理的處理程序都在度假,所以我將不勝感激幫助。
決定更改所使用的插件,現在我嘗試在新插件上復制它,似乎無法將其包裹住以實現它,新的可用代碼如下所示:
<?php global $woocommerce; ?>
<a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>"
title="<?php _e('View your shopping cart', 'woothemes'); ?>">
<?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
$('a.cart').data('count')
給出計數值。
你為什么不這樣做呢? :
$cart = sprintf(_n('%d', cart::$cart_contents_count, 'mini-cart'), cart::$cart_contents_count);
if($cart >= '1')
{
?>
<a href="#" id="shopping-cart" data-count="$cart" class="trigger cart">
<span>
</span>
</a> <?php
}
else
{ ?>
<a href="#" id="shopping-cart" data-count="0" class="trigger cart">
<span>
</span>
</a>
<?php
}
這樣,您就無需使用jQuery
隱藏不需要的元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.