开发者

jquery selecting all elements except the last per group

开发者 https://www.devze.com 2022-12-22 01:35 出处:网络
I have a table that looks like: <table> <tr> <td>one</td><td>two</td><td>three</td><td>last</td>

I have a table that looks like:

<table>
   <tr>
      <td>one</td><td>two</td><td>three</td><td>last</td>
   </tr>
   <tr>
      <td>blue</td><td>red</td><td>green</td><td>last</td>
   </tr>
   <tr>
      <td>Monday</td><td>Tuesday</td><td>Wednesday</td><td>last</td>
   </tr>
</table>

What I want is a jquery selector that will choose all but the last td of each table row. I tried:开发者_StackOverflow

$("tr td:not(:last)").css("background-color","red");
  //changing color just as a test...

But instead of all cells but the last on each row being changed, all cells but the very last one in the table are selected. Similarly, if I change it to:

$("tr td:last").css("background-color","red");

the only one that changes is the very last cell. How do I choose the last (or not last) of each row?


Use this:

$('tr td:not(:last-child)').css('background-color', 'red');

It's saying each <td> that's not the last in that particular <tr>


$('tr').find('td:not(:last)').css('background-color', 'red');

Translates to: for each element in $('tr') selector, do a find() for all tds that are not last.
New result is about tds now, so you can apply .css() as wanted.

0

精彩评论

暂无评论...
验证码 换一张
取 消