简体   繁体   English

根据数值jquery更改div的颜色

[英]change color of div depending numeric value jquery

I have 15 divs with the same class but different id and i want to change the color of the value. 我有15个具有相同类但不同ID的div,并且我想更改值的颜色。

For example, if one or five div's value is under 15 the color of the value will be red, if three or one the value of it is up to 15 but under 45 the color of the value is green and if the value of the div is up to 45 the color will be yellow but all of the colors i want to see at the same time. 例如,如果一个或五个div的值小于15,则该值的颜色将为红色;如果三个或一个div的值小于或等于15,但小于45,则该值的颜色为绿色,并且div的值最多45种颜色将为黄色,但我想同时看到的所有颜色。

My div's are like this: 我的div是这样的:

<div id="listado">
  <div id="cuautitlan" class="dfedo">15</div>
  <div id="coacalco" class="dfedo">54</div>
  <div id="atizapan" class="dfedo">65</div>
  <div id="tlalne" class="dfedo">2</div>
  <div id="tlalne2" class="dfedo">5</div>
  <div id="naucalpan" class="dfedo">90</div>
  <div id="neza" class="dfedo">105</div>
  <div id="huixqui" class="dfedo">65</div>
  <div id="azca" class="dfedo">75</div>
  <div id="gustavo" class="dfedo">45</div>
  <div id="miguel" class="dfedo">35</div>
  <div id="cuauh" class="dfedo">2</div>
  <div id="venus" class="dfedo">1</div>
  <div id="coaji" class="dfedo">58</div>
  <div id="alvaro" class="dfedo">5</div>
  <div id="benito" class="dfedo">95</div>
  <div id="izta" class="dfedo">43</div>
  <div id="magda" class="dfedo">35</div>
  <div id="coyoacan" class="dfedo">33</div>
  <div id="iztapa" class="dfedo">65</div>
  <div id="tlalpan" class="dfedo">89</div>
  <div id="xochi" class="dfedo">99</div>
  <div id="tlahuac" class="dfedo">9</div>
  <div id="milpa" class="dfedo">0</div>
</div>

My jquery is like this 我的jQuery是这样的

$("div.dfedo").each(function()
                  {
                    $(this).value < 15 ? $(this).css('color','red');
                  });

My fiddle 我的小提琴

Make this jQuery plugin: 做这个jQuery插件:

$.fn.colorize = function () {
   return this.each(function() {
      var $this = $(this), number = $this.text();
      $this.css({color: number < 15 ? "red"
                      : number < 45 ? "green"
                      : "yellow"});
   });
};

And just run: 并运行:

$("div.dfedo").colorize();

See DEMO . 参见DEMO

Making a jQuery plugin for things like these is a good habit. 为此类事情制作jQuery插件是一个好习惯。 That way you can reuse your code easily and do interesting things like this: 这样,您可以轻松地重用您的代码并执行如下有趣的操作:

$("div.dfedo").hide().colorize().show("slow");

By making a simple plugin you basically make a new jQuery command that does what you want. 通过制作一个简单的插件,您基本上可以制作一个新的jQuery命令来实现您想要的功能。

Your issue was on the Conditional (ternary) Operator : 您的问题与条件(三元)运算符有关

 $('div.dfedo').each(function() { var $elem = $(this), val = $elem.html(), color = (val < 15) ? 'red' : (val >= 15 && val < 45) ? 'green' : 'yellow'; $elem.css('color', color); }); 
 .dfedo { background-color: #ccc; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="listado> <div id="cuautitlan" class="dfedo">15</div> <div id="coacalco" class="dfedo">54</div> <div id="atizapan" class="dfedo">65</div> <div id="tlalne" class="dfedo">2</div> <div id="tlalne2" class="dfedo">5</div> <div id="naucalpan" class="dfedo">90</div> <div id="neza" class="dfedo">105</div> <div id="huixqui" class="dfedo">65</div> <div id="azca" class="dfedo">75</div> <div id="gustavo" class="dfedo">45</div> <div id="miguel" class="dfedo">35</div> <div id="cuauh" class="dfedo">2</div> <div id="venus" class="dfedo">1</div> <div id="coaji" class="dfedo">58</div> <div id="alvaro" class="dfedo">5</div> <div id="benito" class="dfedo">95</div> <div id="izta" class="dfedo">43</div> <div id="magda" class="dfedo">35</div> <div id="coyoacan" class="dfedo">33</div> <div id="iztapa" class="dfedo">65</div> <div id="tlalpan" class="dfedo">89</div> <div id="xochi" class="dfedo">99</div> <div id="tlahuac" class="dfedo">9</div> <div id="milpa" class="dfedo">0</div> </div> 

Please check the demo : https://jsfiddle.net/pj4c40qq/1/ 请检查演示: https : //jsfiddle.net/pj4c40qq/1/

$("div.dfedo").each(function() {
  $(this).html() < 15 ? $(this).css('color', 'red') : null;

  ($(this).html() >= 15 && $(this).html() < 45) ? $(this).css('color', 'green'): null;

  $(this).html() >= 45 ? $(this).css('color', 'yellow') : null;
});

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

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