删除包含特定词的单元格,例如“ no”漫游器,而不是“ nothing”的词

时间:2019-09-14 08:00:31

标签: javascript jquery html

我用

删除了表行
jQuery('td:contains(No)').parent().hide();

但是包含以“ No”开头的单词的单元格也会被删除。

这是我渲染的html代码:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="data-table" id="product-attribute-specs-table">
     <col width="25%" />
     <col />
     <tbody>
       <tr>
          <th class="label">Zusatz-Info</th>
          <td class="data">No</td>
       </tr>
       <tr>
           <th class="label">Info</th>
           <td class="data">Nothing</td>
       </tr>
     </tbody>
  </table>
<script type="text/javascript">
  jQuery('td:contains(No)').parent().hide();
</script>

如何仅选择包含 No td

感谢jQuery新手的帮助。

2 个答案:

答案 0 :(得分:0)

您不能直接这样做。一种可能的解决方案是使用 data- 属性和Attribute Equals Selector,如下所示:

jQuery('td[data-flag=No').parent().hide();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="data-table" id="product-attribute-specs-table">
   <col width="25%" />
   <col />
   <tbody>
     <tr>
        <th class="label">Zusatz-Info</th>
        <td class="data" data-flag="No">No</td>
     </tr>
     <tr>
         <th class="label">Info</th>
         <td class="data" data-flag="Nothing">Nothing</td>
     </tr>
   </tbody>
</table>

答案 1 :(得分:0)

 $(document).ready(function() {
    $("td").filter(function() {
      if($(this).text() === "No") {
      	$(this).parent().hide();
      }
  	});
 });
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
  <table class="data-table" id="product-attribute-specs-table">
   <col width="25%" />
   <col />
   <tbody>
     <tr>
        <th class="label">Zusatz-Info</th>
        <td class="data" data-flag="No">No</td>
     </tr>
     <tr>
         <th class="label">Info</th>
         <td class="data" data-flag="Nothing">Nothing</td>
     </tr>
   </tbody>
</table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</body>
</html>

尝试一下:

 $(document).ready(function() {
    $("td").filter(function() {
      if($(this).text() === "No") {
        // remove text
        $(this).text('');
        // or hide tr
        $(this).parent().hide();
      }
    });
 });