最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Highlighting columns in a table with jQuery - Stack Overflow

programmeradmin1浏览0评论

I have a table and I am highlighting alternate columns in the table using jquery

$("table.Table22 tr td:nth-child(even)").css("background","blue");

However I have another <table> inside a <tr> as the last row. How can I avoid highlighting columns of tables that are inside <tr> ?

I have a table and I am highlighting alternate columns in the table using jquery

$("table.Table22 tr td:nth-child(even)").css("background","blue");

However I have another <table> inside a <tr> as the last row. How can I avoid highlighting columns of tables that are inside <tr> ?

Share Improve this question edited Jan 30, 2009 at 3:35 AnnanFay 9,75915 gold badges67 silver badges89 bronze badges asked Jan 30, 2009 at 3:15 ViksViks 4136 silver badges10 bronze badges 1
  • Isn't td:even sufficient rather than using nth-child? – cletus Commented Jan 30, 2009 at 11:03
Add a ment  | 

6 Answers 6

Reset to default 7

Qualify it with the > descendant selector:

$("table.Table22 > tbody > tr > td:nth-child(even)").css("background","blue");

You need the tbody qualifier too, as browsers automatically insert a tbody whether you have it in your markup or not.

Edit: woops. Thanks Annan.

Edit 2: stressed tbody.

Untested but perhaps: http://docs.jquery./Traversing/not#expr

$("table.Table22 tr td:nth-child(even)").not("table.Table22 tr td table").css("background","blue");

Here is some code I used to do nested checkbox highlighting within a table. I needed to be able to do a "check all/uncheck all" but only within at a single level within the nesting; that is, I didn't want child elements getting selected as well.

var parentTable = $(this).parents("table:first");
var exclusions = parentTable.find("table :checkbox.select");
var checkboxes = parentTable.find(":checkbox.select").not(exclusions);

I'd get the first table above the current one I was in, get all the checkboxes below this newly found parent table, then exclude them from the plete list of checkboxes I could find. Basically, I was finding every checkbox, but then excluding any child checkboxes I found.

The same could be adapted in your case; replace the checkbox selection with columns instead.

Why not to use the advantages of html ?

Instead of

<table>
  <tr>
    <td>
    ...
    </td>
  </tr>
</table>

Try

<table>
  <tfoot>
    <tr>
       <td>
       ...
       </td>
    </tr>
  </tfoot>
  <tbody>
    <tr>
      <td>
      ...
      </td>
    </tr>
  </tbody>
</table>

You can use the <thead> tag too to manipulate headers.

And now you can call the selector on

$("table.Table22 tbody tr td:nth-child(even)").css("background","blue")

Did you test the following?

$("table.Table22 tr td:nth-child(even):not(:last-child)").css("background","blue")

This page defines a nice function for selecting a column http://programanddesign./js/jquery-select-table-column-or-row/

发布评论

评论列表(0)

  1. 暂无评论