简体   繁体   English

使用Javascript或jQuery动态更改CSS

[英]Dynamically Change CSS with Javascript or jQuery

I have a snippet of code in which I would like to change the css dynamically based on the values being greater than 0. For a value of '0' the class = 'cart-summary__count'. 我有一段代码,其中我想基于大于0的值动态更改css。对于“ 0”的值,类=“ cart-summary__count”。 For a value greater than '0' class = 'cart-summary__count_full'. 对于大于'0'的值,class ='cart-summary__count_full'。

<span class="cart-summary__count" data-v-observable="cart-count">0</span>

<span class="cart-summary__count" data-v-observable="cart-count">1</span>

*edit: *编辑:

<div id="cart-summary">
  <span class="cart-summary__count" data-v-observable="cart-count">0</span>
  <a href class="cart">example.com</a>
</div>

change to: 改成:

<a href class="cart-full">example.com</a>

I am still learning Js and any help would be greatly appreciated. 我仍在学习Js,任何帮助将不胜感激。 The cart value can change on the page when adding an item. 添加商品时,购物车值可以在页面上更改。

Please use this Code 请使用此代码

$(document).ready(function() {
  $("span").each(function(){
     if (parseInt($(this).text()) > 0){
       $(this).removeClass("cart-summary__count");
       $(this).addClass("cart-summary__count_full");
     }
  });
});

Refer this Fiddle 推荐这个Fiddle

Edit 编辑

Based on your edit, use the following code. 根据您的编辑,使用以下代码。

HTML 的HTML

<div id="cart-summary">
  <div class="cartContainer">
    <span class="cart-summary__count" data-v-observable="cart-count">0</span>
    <a href class="cart">example.com</a>  
  </div>
  <div class="cartContainer">
    <span class="cart-summary__count" data-v-observable="cart-count">1</span>
    <a href class="cart">example1.com</a>  
  </div>
</div>

JS JS

$(document).ready(function() {
  $("span").each(function(){
     if (parseInt($(this).text()) > 0){
            $(this).parent().find('a').removeClass("cart");
            $(this).parent().find('a').addClass("cart-full");
     }
  });
});

CSS 的CSS

.cart-full  {
  border : 2px solid red
}

.cartContainer {
  padding-top : 5px;
}

Refer New Fiddle 推荐新Fiddle

Hope this is what you are looking for.I have removed id in your code as the id should be unique and i have added a class cart-summary for all the spans.Once you run the below code it will append the required classes. 希望这就是您想要的东西。我已经在代码中删除了id,因为id应该是唯一的,并且我为所有范围添加了cart-summary类。一旦运行以下代码,它将添加所需的类。

 $('#cart .cart-summary').each(function(){ var span_value = parseInt($(this).text()); if(span_value == 0){ $(this).addClass('cart-summary__count'); } else{ $(this).addClass('cart-summary__count_full'); } }) 
 .cart-summary__count{ color:green; } .cart-summary__count_full{ color:red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="cart"> <span class="cart-summary" data-v-observable="cart-count">0</span> <span class="cart-summary" data-v-observable="cart-count">1</span> <span class="cart-summary" data-v-observable="cart-count">1</span> <span class="cart-summary" data-v-observable="cart-count">1</span> <span class="cart-summary" data-v-observable="cart-count">1</span> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM