简体   繁体   中英

Get all elements that does not contain data attribute

I have a table that looks like this:

<table>
 <tr>
  <td data-col="firstname">Robert</td>
  <td data-col="middlename">E</td>
  <td data-col="lastname">McIntosh</td>
  <td data-col="email">example@gmail.com</td>
  <td data-col="title" data-id="123456">Programmer</td>
 </tr>
 <tr>
  <td data-col="firstname">John</td>
  <td data-col="middlename">M</td>
  <td data-col="lastname">Doe</td>
  <td data-col="email">j.m.doe@gmail.com</td>
  <td data-col="title" data-id="456789">IT Manager</td>
 </tr>
 <tr>
  <td data-col="firstname">Sue</td>
  <td data-col="middlename">L</td>
  <td data-col="lastname">Merser</td>
  <td data-col="email">s.l.merser@gmail.com</td>
  <td data-col="title" data-id="789123">Hates Her Job</td>
 </tr>
</table>

I want to add a class labeled title_hide to all td elements that does not contain the a data attribute id equal to 123456 ?

Javascript I am trying:

$('table tr td:not([data-id="123456"])').closest('tr').addClass('title_hide');

Use :not() selector:

$('td:not([data-id="123456"])').addClass('title_hide');

UPDATE: According to your update it seems that you are trying to add class to <tr> element, which doesn't have <td data-id="123456"> inside. In this case you may use the following approach:

$('tr:not(:has(td[data-id="123456"]))').addClass('title_hide');

DEMO: http://jsfiddle.net/RFwJg/

你可以做:

$("table tr td:not([data-id=123456])").addClass("title_hide");

尝试不()

 $("td:not('[data-col]')").addClass('title_hide');
<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery('td').not('td[data-id="123456"]').addClass('title_hide');
    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM