简体   繁体   中英

Change colour of text based on a value of another div element, Jquery, html and css?

I want to change the color of a text, based on the value of another div element. I need this to run every second so setinterval has to go in there some where

something like this:

<div class="abcd">jkl</div>
<div class="xyz">7</div>
if("xyz" <10){
//change "abcd" to "abcd2" which changes its color}

I'm not good at Jquery but I think thats the tool needed for this job.

$("document").ready(function() {
    var active = window.setInterval(function() {
        $(".abcd").css("backgroundColor",parseInt($(".xyz").text())<7?"#000":"#fff");
    }, 1000);
});

Does it work? Untested.

/e: Ouch, didn't read it that carefully. But I think you'll get the idea, and will be able to use addClass() and removeClass() like in the other answer.

setInterval(function() { 
   var text = $(".xyz").text(); 
   if (parseFloat(text) < 10) {
      $(".abcd").addClass("abcd2").removeClass("abcd");
   }
   }, 1000); //run after every 1 second

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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