如果 <td> 包含文本“0”,则使 <td> 显示 none

If a <td> contains text “0" then make the <td> display none

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

$(".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>

知道为什么吗?

您的代码有效,但如果 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>

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

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

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