如何使用jquery在DOM中查找特定父级

时间:2010-08-27 21:09:05

标签: javascript jquery

我正在尝试匹配以下HTML中的父级

<tbody>
<tr id="group-1" class="group">
 <td>
  <div class="GroupHeader">
    Group1&nbsp;&nbsp;&nbsp;<span class="CriteriaEditOptions"><a id="EditGroupItem" href="/Template/EditGroupItem?GroupID=1">Edit</a></span>&nbsp;&nbsp; <span class="CriteriaMoveUp"><a href="/Template/GroupMovePosition?GroupID=1&amp;MoveType=0&amp;rtn=http%253a%252f%252flocalhost%253a52724%252fTemplate%252fEditTemplate%253fCategoryID%253d6">Up</a></span>&nbsp;&nbsp; <span class="CriteriaMoveDown"><a href="/Template/GroupMovePosition?GroupID=1&amp;MoveType=1&amp;rtn=http%253a%252f%252flocalhost%253a52724%252fTemplate%252fEditTemplate%253fCategoryID%253d6">Down</a></span> &nbsp;&nbsp;<span class="GroupDelete"><a href="/Template/DeleteGroup?GroupID=1&amp;rtn=http%253a%252f%252flocalhost%253a52724%252fTemplate%252fEditTemplate%253fCategoryID%253d6">Delete</a></span>
  </div>
  <table>
  <tbody>
  <tr id="1" class="selection">
   <td>
     Number1&nbsp;&nbsp;&nbsp;<span class="CriteriaEditOptions"><a id="EditCriteriaItem" href="/Template/EditCriteriaItem?CriteriaID=1">Edit</a></span> &nbsp;&nbsp;<a href="/Template/CriteriaItemMovePosition?CriteriaItemID=1&amp;MoveType1=0&amp;rtn=http%253a%252f%252flocalhost%253a52724%252fTemplate%252fEditTemplate%253fCategoryID%253d6">Up</a> &nbsp;&nbsp;<a href="/Template/CriteriaItemMovePosition?CriteriaItemID=1&amp;MoveType1=1&amp;rtn=http%253a%252f%252flocalhost%253a52724%252fTemplate%252fEditTemplate%253fCategoryID%253d6">Down</a> &nbsp;&nbsp;<a href="/Template/DeleteCriteriaItem?CriteriaItemID=1&amp;rtn=http%253a%252f%252flocalhost%253a52724%252fTemplate%252fEditTemplate%253fCategoryID%253d6">Delete</a>
   </td>
  </tr>
  <tr id="2" class="selection">
   <td>
     Price2&nbsp;&nbsp;&nbsp;<span class="CriteriaEditOptions"><a id="EditCriteriaItem" href="/Template/EditCriteriaItem?CriteriaID=2">Edit</a></span> &nbsp;&nbsp;<a href="/Template/CriteriaItemMovePosition?CriteriaItemID=2&amp;MoveType1=0&amp;rtn=http%253a%252f%252flocalhost%253a52724%252fTemplate%252fEditTemplate%253fCategoryID%253d6">Up</a> &nbsp;&nbsp;<a href="/Template/CriteriaItemMovePosition?CriteriaItemID=2&amp;MoveType1=1&amp;rtn=http%253a%252f%252flocalhost%253a52724%252fTemplate%252fEditTemplate%253fCategoryID%253d6">Down</a> &nbsp;&nbsp;<a href="/Template/DeleteCriteriaItem?CriteriaItemID=2&amp;rtn=http%253a%252f%252flocalhost%253a52724%252fTemplate%252fEditTemplate%253fCategoryID%253d6">Delete</a>
   </td>
  </tr>
  <tr id="3" class="selection">
   <td>
     Single Line Text3&nbsp;&nbsp;&nbsp;<span class="CriteriaEditOptions"><a id="EditCriteriaItem" href="/Template/EditCriteriaItem?CriteriaID=3">Edit</a></span> &nbsp;&nbsp;<a href="/Template/CriteriaItemMovePosition?CriteriaItemID=3&amp;MoveType1=0&amp;rtn=http%253a%252f%252flocalhost%253a52724%252fTemplate%252fEditTemplate%253fCategoryID%253d6">Up</a> &nbsp;&nbsp;<a href="/Template/CriteriaItemMovePosition?CriteriaItemID=3&amp;MoveType1=1&amp;rtn=http%253a%252f%252flocalhost%253a52724%252fTemplate%252fEditTemplate%253fCategoryID%253d6">Down</a> &nbsp;&nbsp;<a href="/Template/DeleteCriteriaItem?CriteriaItemID=3&amp;rtn=http%253a%252f%252flocalhost%253a52724%252fTemplate%252fEditTemplate%253fCategoryID%253d6">Delete</a>
   </td>
  </tr>
  <tr id="4" class="selection">
   <td>
     Single Line Text4&nbsp;&nbsp;&nbsp;<span class="CriteriaEditOptions"><a id="EditCriteriaItem" href="/Template/EditCriteriaItem?CriteriaID=4">Edit</a></span> &nbsp;&nbsp;<a href="/Template/CriteriaItemMovePosition?CriteriaItemID=4&amp;MoveType1=0&amp;rtn=http%253a%252f%252flocalhost%253a52724%252fTemplate%252fEditTemplate%253fCategoryID%253d6">Up</a> &nbsp;&nbsp;<a href="/Template/CriteriaItemMovePosition?CriteriaItemID=4&amp;MoveType1=1&amp;rtn=http%253a%252f%252flocalhost%253a52724%252fTemplate%252fEditTemplate%253fCategoryID%253d6">Down</a> &nbsp;&nbsp;<a href="/Template/DeleteCriteriaItem?CriteriaItemID=4&amp;rtn=http%253a%252f%252flocalhost%253a52724%252fTemplate%252fEditTemplate%253fCategoryID%253d6">Delete</a>
   </td>
  </tr>
  <tr id="5" class="selection">
   <td>
     Single Line Text5&nbsp;&nbsp;&nbsp;<span class="CriteriaEditOptions"><a id="EditCriteriaItem" href="/Template/EditCriteriaItem?CriteriaID=5">Edit</a></span> &nbsp;&nbsp;<a href="/Template/CriteriaItemMovePosition?CriteriaItemID=5&amp;MoveType1=0&amp;rtn=http%253a%252f%252flocalhost%253a52724%252fTemplate%252fEditTemplate%253fCategoryID%253d6">Up</a> &nbsp;&nbsp;<a href="/Template/CriteriaItemMovePosition?CriteriaItemID=5&amp;MoveType1=1&amp;rtn=http%253a%252f%252flocalhost%253a52724%252fTemplate%252fEditTemplate%253fCategoryID%253d6">Down</a> &nbsp;&nbsp;<a href="/Template/DeleteCriteriaItem?CriteriaItemID=5&amp;rtn=http%253a%252f%252flocalhost%253a52724%252fTemplate%252fEditTemplate%253fCategoryID%253d6">Delete</a>
   </td>
  </tr>
  <tr id="6" class="selection">
   <td>
     Number6&nbsp;&nbsp;&nbsp;<span class="CriteriaEditOptions"><a id="EditCriteriaItem" href="/Template/EditCriteriaItem?CriteriaID=6">Edit</a></span> &nbsp;&nbsp;<a href="/Template/CriteriaItemMovePosition?CriteriaItemID=6&amp;MoveType1=0&amp;rtn=http%253a%252f%252flocalhost%253a52724%252fTemplate%252fEditTemplate%253fCategoryID%253d6">Up</a> &nbsp;&nbsp;<a href="/Template/CriteriaItemMovePosition?CriteriaItemID=6&amp;MoveType1=1&amp;rtn=http%253a%252f%252flocalhost%253a52724%252fTemplate%252fEditTemplate%253fCategoryID%253d6">Down</a> &nbsp;&nbsp;<a href="/Template/DeleteCriteriaItem?CriteriaItemID=6&amp;rtn=http%253a%252f%252flocalhost%253a52724%252fTemplate%252fEditTemplate%253fCategoryID%253d6">Delete</a>
   </td>
  </tr>
  </tbody>
  </table>
 </td>
</tr>
</tbody>

我位于

<div class="GroupHeader">

我正在尝试匹配

的父母
<tr id="group-1" class="group">

有没有办法在jquery中执行此操作?我已尝试使用parent(),但无效

3 个答案:

答案 0 :(得分:4)

您可以使用closest('tr')获取与提供的选择器匹配的第一个父级。

答案 1 :(得分:2)

.parents('tr')向上寻求

答案 2 :(得分:2)

parent只看一级。使用parents查看多个级别。

$current.parents("#group-1 .group")