简体   繁体   English

在jQuery中查找遥远的父母

[英]Find distant parent in jquery

I'm a little stuck on how to track down an element that's up the tree as a parent element. 我对如何跟踪树上作为父元素的元素有些困惑。

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

The .product-card class is not the direct parent but further up the tree. .product-card类不是直接父类,而是更远的树。 I need animate it based on the hover method on the .btn-action class. 我需要基于.btn-action类的悬停方法对其进行.btn-action Can this be achieved with .parent() or .find() ? 可以使用.parent().find()来实现吗?

To go up the tree you can use .closest(). 要上树,您可以使用.closest()。

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

To go further in the tree use .find(). 要在树中更进一步,请使用.find()。 To stay on the same level, you can use .siblings() 要保持相同级别,可以使用.siblings()

Although I am not sure about the space in your classname. 尽管我不确定您的班级名称中的空格。 Try using a dash if multiple classes wasn't intended. 如果不需要多个类,请尝试使用破折号。

我认为,您可以使用.closest( selector )方法来获取元素的最近父级。

Have you tried using .parents()? 您是否尝试过使用.parents()? @see https://api.jquery.com/parents/ @请参阅https://api.jquery.com/parents/

To get the .product-card ancestors try: 要获取.product卡祖先,请尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM