Colorize negative/positive numbers (jQuery)

一笑奈何 提交于 2019-12-09 22:55:26

问题


I'd like to color numbers in a table for better readability: 

  • green for positive (+00.00);
  • red for negative (-00.00) and;
  • black for default case (no sign)

回答1:


Here ya go:

$(document).ready( function() {

  // the following will select all 'td' elements with class "of_number_to_be_evaluated"
  // if the TD element has a '-', it will assign a 'red' class, and do the same for green.

  $("td.of_number_to_be_evaluated:contains('-')").addClass('red');
  $("td.of_number_to_be_evaluated:contains('+')").addClass('green');
}

Then use CSS to style the input elements:

td.red {
  color: red;
}

td.green {
  color: green;
}



回答2:


Firstly, the best way to do this if the numbers are static is on the serverside. Assign a class based on value:

<td class="positive">+34</td>
<td class="negative">-33</td>

with:

td { color: black; }
td.positive { color: green; }
td.negative { color: red; }

(or be more selective if you need to be).

But if you must do this on the client, I might suggest:

$("td").each(function() {
  var text = $(this).text();
  if (/[+-]?\d+(\.\d+)?/.test(text)) {
    var num = parseFloat(text);
    if (num < 0) {
      $(this).addClass("negative");
    } else if (num > 0) {
      $(this).addClass("positive");
    }

  }
});

You may need to adjust the regular expression depending on what kinds of numbers you want to catch (eg 1.2e11 or 3,456).

Why the regex and not just parseFloat()? Because:

parseFloat("34 widgets");

returns 34. If this is fine then use that and skip the regex test.




回答3:


The css:

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

The markup

<table>
  <tr><td>+11.11</td><td>-24.88</td><td>00.00</td></tr>
  <tr><td>-11.11</td><td>4.88</td><td>+16.00</td></tr>
</table>

The code

$('td').each(function() {
  var val = $(this).text(), n = +val;
  if (!isNaN(n) && /^\s*[+-]/.test(val)) {
    $(this).addClass(val >= 0 ? 'pos' : 'neg')
  }
})



回答4:


CSS only, without javascript solution. I found it here http://rpbouman.blogspot.ru/2015/04/css-tricks-for-conditional-formatting.html

    /* right-align monetary amounts */
    td[data-monetary-amount] {
      text-align: right;
    }

    /* make the cells output their value */
    td[data-monetary-amount]:after {
      content: attr(data-monetary-amount);
    }

    /* make debit amounts show up in red */
    td[data-monetary-amount^="-"]:after {
      color: red;
    }
  <table border="1">

   <tr>
    <th>Gain</th>
    <td data-monetary-amount="$100"></td>
   </tr>

   <tr>
    <th>Losst</th>
    <td data-monetary-amount="-$100"></td>
   </tr>

  </table>



回答5:


Here is the more complete solution:

<script>
$(document).ready( function() {
// get all the table cells where the class is set to "currency" 
$('td.currency').each(function() {
//loop through the values and assign it to a variable 
    var currency = $(this).html();
//strip the non numeric, negative symbol and decimal point characters
// e.g. Spaces and currency symbols 
    var val = Number(currency.replace(/[^0-9\.-]+/g,""));
// check the value and assign class as necessary 
// (I'm sure this could be done with a switch statement 
    if(val > 0) {
        $(this).addClass('positive');
    }
    if(val < 0) {
        $(this).addClass('negative');
    }
    })
})
</script>

Thanks Alun Rowe for providing this code at http://www.alunr.com/articles/jquery-addclass-to-positive-and-negative-values-on-a-page




回答6:


Set up the currency-field class on the td and listening for change event on that td then depending on the value it should add the proper css class to change the color.



来源:https://stackoverflow.com/questions/1824266/colorize-negative-positive-numbers-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!