jQuery选择每个组中除最后一个元素外的所有元素 [英] jquery selecting all elements except the last per group

查看:537
本文介绍了jQuery选择每个组中除最后一个元素外的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的表

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>

我想要的是一个jQuery选择器,它将选择每个表行的最后一个td以外的所有内容.我试过了:

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

$("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?

推荐答案

使用此:

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

这是不是每个<tr>的最后一个<td>

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

这篇关于jQuery选择每个组中除最后一个元素外的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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