如果行的第一个td没有具有给定值的ID,则用jQuery删除所有行 [英] Jquery-Delete all the row if first td of row does not have the id with given value

查看:71
本文介绍了如果行的第一个td没有具有给定值的ID,则用jQuery删除所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有很多行的表.我要从中删除除第一行具有给定ID的行以外的所有行.

I have a table with many rows. from which i want to delete all the rows except the row, of which first td has a given id.

我半途而废

 <tr>
   <td id="1" class="td_link" onclick="viewPointDetails(1)">Barrackpur</td>
   <td class="td_link" id="11" class="edit_client" onclick="editPoint(1)">India</td>
 </tr>


 <tr>
   <td id="2" class="td_link" onclick="viewPointDetails(2)">Madiwala</td>
   <td class="td_link" id="11" class="edit_client" onclick="editPoint(1)">India</td>
 </tr>

和我的jquery;

and my jquery;

   $('tr').each(function () {   
       if first td does not have given  id say 1  then delete this row.
   });  

我的第一行是标题.所以我不希望对此进行检查(不希望此行被删除).

my First row is header. so i dont want this to be checked(do not want this row to be deleted).

推荐答案

您应该能够在没有each循环的情况下实现此目标:

You should be able to achieve this without the each loop:

$("tr:not(:has(#1))").remove();

这使用 :has 选择器返回包含与选择器匹配的元素的行.它使用 :not 选择器得到相反的结果(因此,最终所有 not 的行包含与选择器匹配的元素),然后使用 remove 删除匹配集中的所有元素.

This uses the :has selector to return rows which contain an element matching the selector. It uses the :not selector to get the opposite of that (so you end up with all rows that do not contain an element matching the selector), and then uses remove to remove all elements in the matched set.

这篇关于如果行的第一个td没有具有给定值的ID,则用jQuery删除所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆