jQuery选择器除了第一个以外的每个表上的第一行之外的每一行

时间:2021-02-07 22:19:45

I want to apply right alignment on the last cell of every table row, skipping the first table on the page and skipping the first row of every table.

我想在每个表行的最后一个单元格上应用右对齐,跳过页面上的第一个表并跳过每个表的第一行。

I wrote the following:

我写了以下内容:

$("table:gt(0) tr:gt(0) td:last-child").css("text-align", "right");
  1. The tables after the first are selected. GOOD.
  2. 选择第一个之后的表。好。

  3. The last cell in each row is aligned right. GOOD.
  4. 每行中的最后一个单元格对齐。好。

  5. The first row in the first table in the result set is skipped. GOOD.
  6. 跳过结果集中第一个表中的第一行。好。

  7. Each first row in subsequent tables in the result set has the style applied. BAD.
  8. 结果集中后续表中的每个第一行都应用了样式。坏。

I've tried passing a function to the "each" method on the wrapped set, but that isn't working. Any ideas?

我已经尝试将函数传递给包装集上的“each”方法,但这不起作用。有任何想法吗?

5 个解决方案

#1


You were almost there.

你快到了。

$("table:gt(0)").find("tr:gt(0) td:last-child").css("text-align", "right");

otherwise, tr:gt(0) is always true because jquery is looking at each tr, not each tr in each table.

否则,tr:gt(0)总是为真,因为jquery正在查看每个tr,而不是每个表中的每个tr。

#2


try

$("table:gt(0) tr:not(:first-child) td:last-child").css("text-align", "right");

as the gt(0) only refers to the index in the jQuery object that you're building, not the table.

因为gt(0)只引用你正在构建的jQuery对象中的索引,而不是表。

#3


what's wrong with using the DOM to select your tables, and using jQuery to apply your styles to the tables? too many lines of code?

使用DOM选择表,并使用jQuery将样式应用于表有什么问题?代码行太多了?

#4


$("table:gt(0) tr").each(){
     if($(this).not(":first-child")){
           $(this).find("td:last-child").css("text-align","right");
     }
}

not the best way probably, but this is how I would do it.

可能不是最好的方式,但这就是我要做的。

#5


There is probably a better way, but one idea may be to add a class to the first row of each table that you do not want to have the style and filter those out with a not(.myclass).

可能有更好的方法,但有一个想法可能是在每个表的第一行添加一个类,你不想拥有该样式,并使用not(.myclass)过滤掉那些。

Likewise add classes to the tables that you do want the styles to go to.

同样,将类添加到您希望样式转到的表中。

#1


You were almost there.

你快到了。

$("table:gt(0)").find("tr:gt(0) td:last-child").css("text-align", "right");

otherwise, tr:gt(0) is always true because jquery is looking at each tr, not each tr in each table.

否则,tr:gt(0)总是为真,因为jquery正在查看每个tr,而不是每个表中的每个tr。

#2


try

$("table:gt(0) tr:not(:first-child) td:last-child").css("text-align", "right");

as the gt(0) only refers to the index in the jQuery object that you're building, not the table.

因为gt(0)只引用你正在构建的jQuery对象中的索引,而不是表。

#3


what's wrong with using the DOM to select your tables, and using jQuery to apply your styles to the tables? too many lines of code?

使用DOM选择表,并使用jQuery将样式应用于表有什么问题?代码行太多了?

#4


$("table:gt(0) tr").each(){
     if($(this).not(":first-child")){
           $(this).find("td:last-child").css("text-align","right");
     }
}

not the best way probably, but this is how I would do it.

可能不是最好的方式,但这就是我要做的。

#5


There is probably a better way, but one idea may be to add a class to the first row of each table that you do not want to have the style and filter those out with a not(.myclass).

可能有更好的方法,但有一个想法可能是在每个表的第一行添加一个类,你不想拥有该样式,并使用not(.myclass)过滤掉那些。

Likewise add classes to the tables that you do want the styles to go to.

同样,将类添加到您希望样式转到的表中。