jquery选择器如何获取父元素的内容

时间:2010-11-07 05:56:32

标签: jquery jquery-selectors

$("input[name='add'].my").click(function(){
    //i tried use this <input type="button" name="add" value=..../> 
    //element to find his parent's parent the `<div>` object
    //how can i get it  

});


<div>
  <tr>
    <td><%= post.title %></td>
    <td><%= post.content %></td> 
    <td><%= link_to 'Show', post %></td>
    <td><%= link_to 'Edit', edit_post_path(post),:remote=>true %></td>
    <td><%= link_to 'Destroy', post, :confirm => 'Are you sure?',:remote=>true, :method => :delete %></td>
    <td><input type="button" name="add" value="add" class="my"/></td>
    <td></td>
  </tr>
</div>
<div>
  <tr>
    <td><%= post.title %></td>
    <td><%= post.content %></td> 
    <td><%= link_to 'Show', post %></td>
    <td><%= link_to 'Edit', edit_post_path(post),:remote=>true %></td>
    <td><%= link_to 'Destroy', post, :confirm => 'Are you sure?',:remote=>true, :method => :delete %></td>
    <td><input type="button" name="add" value="add" class="my"/></td>
    <td></td>
  </tr>
</div>

2 个答案:

答案 0 :(得分:3)

这将为您提供最近的父div:

$("input[name='add'].my").click(function(){
  var parentDiv = jQuery(this).closest("div");
});

答案 1 :(得分:1)

你走了:

$(function() {
   $("input[name='add'] .my").click(function(){
      var parent = $(this).parent().parent().parent();
      //OR
      var parent = $(this).closest('div');
      //do stuff...
   });
});