【问题标题】:Change background-color based on value in 3rd td根据 3rd td 中的值更改背景颜色
【发布时间】:2021-03-30 02:05:31
【问题描述】:

我想根据行的第三列中包含的值更改背景颜色

<tr><td>0</td><td>1</td><td>FAIL</td></tr>

为什么这不起作用(我正在使用 jQuery):

<script>
$(document).ready(function(){
    $("tr td:nth-child(3)").map(function () {
  if (parseInt($(this).text()) === 'FAIL') $(this).css("background-color", "red")
})
});
</script>

【问题讨论】:

    标签: jquery jquery-selectors


    【解决方案1】:

    不需要parseInt,因为您正在比较字符串:

    $(document).ready(function(){
      $("tr td:nth-child(3)").map(function () {
        if ($(this).text() === 'FAIL') 
          $(this).css("background-color", "red");
      });
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <table>
      <tbody>
        <tr>
          <td>0</td>
          <td>1</td>
          <td>FAIL</td>
        </tr>
      </tbody>
    </table>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-24
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      相关资源
      最近更新 更多