【问题标题】:If a <td> contains text “0" then make the <td> display none [closed]如果 <td> 包含文本“0”,则使 <td> 显示无 [关闭]
【发布时间】:2019-03-07 08:08:18
【问题描述】:

我正在尝试使用 jQuery 使 td 在包含文本 0 时消失。我试过了,但没有用:

$(".detail-parameters tr:contains('0')").css("display", "none");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="detail-parameters">
  <tbody>
    <tr>
      <th>some text</th>
      <td>0</td>
    </tr>
    <tr>
      <th>some text</th>
      <td>1</td>
    </tr>
  </tbody>
</table>

任何想法为什么?

【问题讨论】:

  • 它在这里工作jsfiddle.net/GCu2D/5751
  • 我将您的代码放在一个 sn-p 中,它工作得非常好(尽管您隐藏了整个 tr,而不是 td)。检查控制台是否有错误
  • @kuri 你的代码可以工作,但如果td0110 文本也会隐藏它。
  • 感谢大家的快速回复,如果我想隐藏 td 时只包含“0”怎么办?

标签: javascript jquery html-table contains


【解决方案1】:

您的代码有效,但如果 td 在文本中有 0110,则代码将其隐藏是不正确的。所以你应该检查.filter()中元素的文本,而不是使用:contain()

$(".detail-parameters tr").filter(function(){
  return $(this).find("td").text() == 0;
}).css("display", "none");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="detail-parameters">
  <tbody>
    <tr>
      <th>some text</th>
      <td>0</td>
    </tr>
    <tr>
      <th>some text</th>
      <td>1</td>
    </tr>
    <tr>
      <th>some text</th>
      <td>01</td>
    </tr>
  </tbody>
</table>

【讨论】:

  • 它工作正常,谢谢!
【解决方案2】:

你正在使用 tr 而不是 td 试试这个

$(document).ready(function(){
  $(".detail-parameters td:contains('0')").css("display", "none");
});

但您的代码也会隐藏 10 或 01。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    相关资源
    最近更新 更多