如何在php或javascript中为数字着色[关闭]

I am developing a portfolio as a project to learn PHP and JavaScript. I have the stock price updating every minute but I would like to make it go green or red depending if it is going up or down. What would be the best way to accomplish this?

Look into applying CSS on the element containing the stock price. This can be done dynamically using Javascript. You can tell whether it has gone up or down by keeping a record of the last value and comparing it to the new. Good luck.

I would do that as followed:

  1. Put the number in a e.g. div
  2. Get the new number via ajax
  3. Look if the new number is < , === or > than the current
  4. Style the div

This is not to complicated with a little CSS and some JavaScript. Here is a basic example. This could be done in a number of different ways. This is just one approach.

CSS:

.green {color:green;}
.red {color:red;}

HTML:

<span class="stock-value green">12</span>

JavaScript (using jQuery):

if( $('.stock-value').text() > 0) {
   $('stock-value').addClass('green');
}
else if($('.stock-value').text() < 0){
    $('stock-value').addClass('red');
}

DEMO:

http://jsfiddle.net/XakPM/