繁体   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