html - 数字大于的 CSS 规则

标签 html css numbers

<table>
<tbody>
<tr class="positive">
<td class="happy">12</td>
<td class="happy">7</td>
<td class="happy">69</td>
</tr>
</tbody>
</table>

▲ 这是原始html代码。

td.happy(@>10) {color:red;}
td.happy(@>50) {color:blue;}

▲ 这就是我想要的CSS代码。 (想象)

我想在数字大于特定数字(10, 50)时应用CSS样式

我已经尝试过 Java 脚本,它确实有效,但我想尽可能只使用 html 和 CSS 制作网站。

有什么方法可以选择标签中的文本编号吗?

最佳答案

你不能用纯css,你必须jquery的帮助:

$(document).ready(function(){
  $('tr.positive td.happy').each(function(){
    number = parseFloat($(this).text());
    if(number > 10)
      $(this).addClass('greater10');
    if(number > 50)
      $(this).addClass('greater50');
  })
})
td {
  border: 1px solid #000;
}

.greater10 {
  color: red;
}

.greater50 {
  color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<table>
  <tbody>
    <tr class="positive">
      <td class="happy">12</td>
      <td class="happy">7</td>
      <td class="happy">69</td>
    </tr>
  </tbody>
</table>

关于html - 数字大于的 CSS 规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47238571/

相关文章:

html - 如何将一个div放在另一个div之上

javascript - 可以在 Jquery 中使用变量作为后代选择器吗?

html - 有没有一种简单的方法可以使 html textarea 和 input type 文本同样宽?

HTML5、CSS : Make FA icon and link break correctly on mobile

html - 如何使用没有白色边框的CSS居中图像

php - 如何使用 PHP 将动态文本框保存到数据库

javascript - 如何使用脚本通过电子邮件发送已解析的 JSON

perl - 如何在 Perl 中将数字转换为英文形式?

shell - bash 中的 Grep 数字范围

php - 按数值排列的 Glob 数组