添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I want to get the <tr> element of a table by knowing the ID of one of it's elements (div).

Here's what the table looks like in HTML:

<table id="table-0">
  <thead>
  <tr role="row">
   <th>house</th>
   <th>phone</th>
  </thead>
 <tbody>
 <tr class="odd">
  <td class="">
    <div id="56"></div>my house
  <td class="">
    <div id="57"></div>1234
</tbody>
</table>

So I would like to get the <tr> element that has a <td> element that contains a <div> with the ID 56 (in this example).

Here's what I tried in jQuery (data['id'] = 56):

 var tr = $("tr:has(td:has($(\"#\" + data['id']))))");
                You have two elements with the same id - this is invalid in HTML. You need to use classes for grouped identifiers.
– Rory McCrossan
                Nov 14, 2013 at 10:39
                This way the guy will still use a Duplicated ID on the HTML, this is incorrect for HTML Standards.
– Paulo Roberto Rosa
                Nov 14, 2013 at 11:05

First: is totally invalid to use same ID to more of one element because the ID is Unique. Its a HTML Standard.

But if you want to get the selector for your element you can use this (without needing ID):

$('tr.odd').children().children();

This is the correct way because you dont need a Duplicated ID.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.