在jQuery中选择祖先的有效方法

khe*_*lll 2 jquery jquery-selectors

给出以下HTML代码段:

<div class="image">
  <div class="placeholder">
    <a class="load iframe" href="https://contestapp.com/images/4"><img src="/uploads/image/image_file/4/thumb_he_sport_nutrition_for_active_kids_photo.jpg?1307271623" alt="Thumb_he_sport_nutrition_for_active_kids_photo"></a>
  </div>
  <div class="user">By </div>
  <div class="actions">
      <span>
        <a class="vote-link" href="/images/4/vote"></a>
      </span>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

我有一个类投票链接的链接,我想选择链接a.load和a.load img,我怎么能通过jQuery有效地做到这一点?

lon*_*day 6

首先,你需要和一个共同的祖先一起去closest.

$(this).closest('div.image');
Run Code Online (Sandbox Code Playgroud)

然后,你需要找到的链接,并使用图像find时,多选择后代选择:

$(this).closest('div.image').find('a.load, a.load img');
Run Code Online (Sandbox Code Playgroud)