简体   繁体   English

使用jQuery遍历父树

[英]Traversing through parent tree with jQuery

The question I have surrounds traversing an element's parents and triggering a CSS class on multiple elements. 我所遇到的问题涉及遍历元素的父级并在多个元素上触发CSS类。

Consider I have the following: 考虑一下我有以下几点:

<article class="portfolio type-portfolio">
<figure>
  <a class="recent-main-category" href="#">
    <span class="image_container_img">
        <img class="fullwidth" src="/wp-content/themes/qreator/images/community.jpg" />
    </span>
  </a>
</figure>

<header class="entry-header">
  <h4 class="entry-title p_name">
    <a href="#" class="main-category-title">
      Hosting Community
    </a>
  </h4>

<div class="entry-content">             
 <div class="recent-in-category">
 <a href="<?php echo the_permalink(); ?>"><?php echo the_title(); ?></a>
 </div>
</div>
</article>

What I want to do is trigger a CSS class to be temporarily applied on mouseover of the image (".fullwidth"). 我想做的是触发一个CSS类,将其临时应用于图像的鼠标悬停(“ .fullwidth”)。 The same class would also be applied to the item title ".main-category-title" 相同的类别也将应用于项目标题“ .main-category-title”

I would also like this trigger to work vice versa, as in, a mouseover of ".main-category-title" will trigger the effect on ".fullwidth" (the image element). 我也希望此触发器反之亦然,例如,将鼠标悬停在“ .main-category-title”上将触发对“ .fullwidth”(图像元素)的影响。

I am hoping this image might explain more clearly: 我希望这张图片可以更清楚地说明: 在此处输入图片说明

It's easy with http://api.jquery.com/hover/ 使用http://api.jquery.com/hover/很容易

 $(".fullwidth, .main-category-title").hover(function(){
    $(".fullwidth, .main-category-title").addClass('tempClass');
 },function(){
    $(".fullwidth, .main-category-title").removeClass('tempClass');
 });

Try 尝试

$('.type-portfolio').find('.fullwidth, .main-category-title').hover(function(){
    $(this).closest('.type-portfolio').find('.fullwidth, .main-category-title').addClass('selected')
}, function(){
    $(this).closest('.type-portfolio').find('.fullwidth, .main-category-title').removeClass('selected')
})

Demo: Fiddle 演示: 小提琴

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

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