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

javascript - How do I find a table of certain class with jquery selectors - Stack Overflow

programmeradmin2浏览0评论

How do I select just a table with class d with jquery selectors. For some reason this code won't work properly...

var dTableTags = $(".d table");

example table would be...

<table id="thetable" class="d">
  <thead>
    <tr><th>Column 1 header</th><th>Column 2 header</th></tr>
  </thead>
  <tbody>
    <tr><td>Column 1 data</td><td>Column 2 data</td></tr>
  </tbody>
</table>

How do I select just a table with class d with jquery selectors. For some reason this code won't work properly...

var dTableTags = $(".d table");

example table would be...

<table id="thetable" class="d">
  <thead>
    <tr><th>Column 1 header</th><th>Column 2 header</th></tr>
  </thead>
  <tbody>
    <tr><td>Column 1 data</td><td>Column 2 data</td></tr>
  </tbody>
</table>
Share Improve this question asked Nov 8, 2012 at 18:09 MTAG11MTAG11 3962 gold badges8 silver badges23 bronze badges 1
  • 1 Honestly, there was some guessing involved. In my mind I was thinking select all elements of class d, and then the tables associated from that list. :\ – MTAG11 Commented Nov 8, 2012 at 18:29
Add a comment  | 

3 Answers 3

Reset to default 18

Your selector is wrong; try $("table.d") instead.

The jQuery documentation does not explain this directly, it defers to the W3C CSS selector documentation which is a lot more comprehensive.

You are trying to search for table that is inside class d Which is wrong ..

Change your selector to this

$("table.d");   // Because the table has the class d

If you have an ID property/attribute set on the element, why are you selecting by class, anyways? Selecting by ID is much better if you're trying to select something in a distinct fashion. CSS classes can be shared, but the ID property, not so much.

Thus, your selector should be $('#thetable')

Anything else is superfluous really.

发布评论

评论列表(0)

  1. 暂无评论