簡體   English   中英

在jQuery中查找遙遠的父母

[英]Find distant parent in jquery

我對如何跟蹤樹上作為父元素的元素有些困惑。

$('.btn-action').hover(
  function(){
    $(this).find('.product-card').addClass('animated bounce');
  },
  function(){
    $(this).find('.product-card').removeClass('animated bounce')
  }
);

.product-card類不是直接父類,而是更遠的樹。 我需要基於.btn-action類的懸停方法對其進行.btn-action 可以使用.parent().find()來實現嗎?

要上樹,您可以使用.closest()。

$(this).closest('.product-card').addClass('animated bounce');

要在樹中更進一步,請使用.find()。 要保持相同級別,可以使用.siblings()

盡管我不確定您的班級名稱中的空格。 如果不需要多個類,請嘗試使用破折號。

我認為,您可以使用.closest( selector )方法來獲取元素的最近父級。

您是否嘗試過使用.parents()? @請參閱https://api.jquery.com/parents/

要獲取.product卡祖先,請嘗試:

$('.product-card').parents()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM