【问题标题】:Remove cells containig specific word like "no" bot not words with "nothing" [duplicate]删除包含特定单词的单元格,例如“no”机器人而不是带有“nothing”的单词 [重复]
【发布时间】:2020-01-15 22:01:31
【问题描述】:

我已经用

删除了表格行
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>

如何只选择包含 Notd

感谢 jQuery 新手的帮助。

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    你不能那样直接。一种可能的解决方案是使用 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>

    【讨论】:

      【解决方案2】:

       $(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();
            }
          });
       });
      

      【讨论】:

      • 您好 Fiodorov Andrei,我不明白您的“$”符号。我一开始使用 jQuery。你为什么用美元?我使用的代码来自 Magento 1.9 Shop - 还有prototype.js、scriptaculous、js 和jquery.js。你能解释一下我应该在哪里使用 jQuery 或 $?
      • 你好。我的解决方案基于没有数据属性的html。
      • 这是我在控制台中得到的:Uncaught TypeError: $(...).text is not a function at HTMLTableCellElement. (.html:727) at jquery-1.9 .1.min.js:4
      • 试试这个:$(document).ready(function() { $("td").filter(function(index, cell) { if(cell.innerHTML === "No") { $(this).parent().hide(); } }); });
      • 在线代码example
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 2021-03-19
      • 1970-01-01
      相关资源
      最近更新 更多