【问题标题】:Toggle class upon change of data attribute更改数据属性时切换类
【发布时间】:2013-01-22 02:58:15
【问题描述】:

我正在处理一个购物车,我的客户希望它在空的时候有一个背景,而当它有物品时有另一个背景。所以这就是我得到的标记:

<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>

【问题讨论】:

    标签: javascript php jquery html woocommerce


    【解决方案1】:
    $('a.cart').data('count')
    

    给出count的值。

    【讨论】:

      【解决方案2】:

      你为什么不这样做? :

      $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来隐藏不需要的元素了。

      【讨论】:

      • 问题是,我不能弄乱数据计数它有插件的代码来提供购物车有多少物品我只是把 0 放在那里作为例子,它有这样的东西: data-count=""
      • 无法理解它,决定我们将切换到另一个商务插件并且它停止工作,我认为它会以相同的方式工作但没有,我会很感激任何额外的帮助
      • 检查你的PHP版本..也许它与..除此之外..我无法帮助你:(..代码应该可以工作..因为它根本没有问题!
      猜你喜欢
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      相关资源
      最近更新 更多