简体   繁体   English

根据div值更改CSS值

[英]Changing CSS value based on div value

I am trying to change the background of a cell in a css grid based on a value of a div that is pulled from a php database query.I'm using jquery to achieve the same function with another div, but the only difference is the value that is being compared is the same div in the css. 我正在尝试根据从php数据库查询中提取的div的值更改css网格中单元格的背景。我正在使用jquery与另一个div实现相同的功能,但唯一的区别是要比较的值是css中的相同div。 Here is the div structure for the html. 这是html的div结构。 box datetime is a child of box a . box datetimebox a的子级。

<div class='box a'>
<div class='box name'><a style='text-decoration:none; color:#444;' href='foobar.com' target='_blank'>Proper Name</a></div>
<div class='box mm'>75.49</div>
<div class='sog'>10.2</div>
<div class='box datetime'>1</div>

Here is the function i'm using. 这是我正在使用的功能。

$('.datetime').each(function(index, value) {

  var value = Number($(this).text());
  console.log(value);
  if (value < 4)
     $('.a').css('background-color', '#fff');
     else if (value >= 4 && value <= 15)
     $('.a').css('background-color', 'orange');
     else
     $('.a').css('background-color', 'purple');   

});
  • .datetime is a whole number that represents the number of minutes bewteen 2 times. .datetime是一个整数,表示2倍之间的分钟数。
  • .a is the css grid cell that i want to change the css properties of. .a是我要更改其css属性的css网格单元。

Right now, it will only turn all of the .a background color to #fff. 目前,它只会将所有.a背景颜色变成#fff。 It will not evaluate correctly. 它将无法正确评估。 What am I doing wrong? 我究竟做错了什么?

Try this: 尝试这个:

  var value = Number($('.datetime').text());
  //console.log(value);
  if (value < 4)
     $(".a", '.datetime').css('background-color', '#fff');
  else if (value >= 4 && value <= 15)
     $(".a", '.datetime').css('background-color', 'orange');
  else
     $(".a", '.datetime').css('background-color', 'purple');   

..if you have a single group you dont need the each ..如果您只有一个小组,则不需要每个小组

You're setting styling on all .a elements. 您正在所有.a元素上设置样式。 The last value in the loop determines the value for all .a elements. 循环中的最后一个值确定所有.a元素的值。 To set them individually for each .datetime , get the parent ( .a ) element of each .datetime : 要单独设置他们每个.datetime ,得到父( .a各)元素.datetime

$('.datetime').each(function(index, value) {

  var value = Number($(this).text());
  console.log(value);

  var $elem = $(this).parent();

  if (value < 4)
     $elem.css('background-color', '#fff');
  else if (value >= 4 && value <= 15)
     $elem.css('background-color', 'orange');
  else
     $elem.css('background-color', 'purple');   

});

You are passing value in from .each but overwriting the value on the next line. 您正在从.each传递值,但覆盖了下一行的值。 Also you don't need to check that the value is <= 4 since you've already ruled out that the value isn't < 4 . 另外,由于您已经排除了值不< 4情况,因此无需检查该值是否<= 4等于< 4 But I think the reason your code isn't working is because you are changing all .a to the color dictated by the last element processed: 但是我认为您的代码无法正常工作的原因是因为您将所有.a更改为最后处理的元素所指定的颜色:

 $('.datetime').each(function(index, value) { var textValue = parseInt($(value).text()); if (textValue < 4) { $(value).parent(".a").css('background-color', '#fff'); } else if (textValue <= 15) { $(value).parent(".a").css('background-color', 'orange'); } else { $(value).parent(".a").css('background-color', 'purple'); } }); 
 body { background-color: green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='box a'> <div class='box name'><a style='text-decoration:none; color:#444;' href='foobar.com' target='_blank'>Proper Name</a></div> <div class='box mm'>75.49</div> <div class='sog'>10.2</div> <div class='box datetime'>3</div> </div> <div class='box a'> <div class='box name'><a style='text-decoration:none; color:#444;' href='foobar.com' target='_blank'>Proper Name</a></div> <div class='box mm'>75.49</div> <div class='sog'>10.2</div> <div class='box datetime'>12</div> </div> <div class='box a'> <div class='box name'><a style='text-decoration:none; color:#444;' href='foobar.com' target='_blank'>Proper Name</a></div> <div class='box mm'>75.49</div> <div class='sog'>10.2</div> <div class='box datetime'>18</div> </div> 

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

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