在jquery中查找远方父级



我有点纠结于如何追踪树上作为父元素的元素。

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

.product-card类不是直接父类,而是树的更上一层楼。我需要根据.btn-action类上的悬停方法对其进行动画处理。这可以通过.parent().find()来实现吗?

要上树,你可以使用 .closest((。

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

要在树中走得更远,请使用 .find((。 要保持在同一水平上,您可以使用 .siblings((

虽然我不确定你类名中的空格。如果不希望使用多个类,请尝试使用破折号。

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

你试过使用 .parents(( 吗? @see https://api.jquery.com/parents/

要获取 .product 卡祖先,请尝试:

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

最新更新