javascript - 将类添加到单个元素而不是全部在悬停时使用 JQuery

标签 javascript jquery html css

当我将鼠标悬停在我页面上的 anchor 标记上时,我试图将一个类添加到一个图标。

但是当我悬停时,它会将悬停类应用于所有元素,并带有图标类。我怎样才能将其选中,以便它只针对我悬停在其中的列中的图标。

提前致谢。

$('.column-content-link a').hover(function(){
   $('.column-content .icon').toggleClass('hover');
   $('.column-content h3').toggleClass('hover');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="column-content">
        <div class="column-content-header">
                <i class="icon fa fa-rocket"></i>
            <h3 class="">Heading 1</h3>
            <p><span>Blue bottle heirloom cronut, iceland salvia austin swag af photo booth church-key vinyl yuccie pour-over venmo bitters. Drinking vinegar humblebrag banh mi godard. Cronut lyft actually iceland, pabst chia small batch fam. Aesthetic organic ennui stumptown. </span></p>
        </div>
        <div class="column-content-link">
                <a href="https://www.google.com">Learn more</a>
        </div>
</div>

<div class="column-content">
        <div class="column-content-header">
                <i class="icon fa fa-rocket"></i>
            <h3 class="">Heading 2</h3>
            <p><span>Blue bottle heirloom cronut, iceland salvia austin swag af photo booth church-key vinyl yuccie pour-over venmo bitters. Drinking vinegar humblebrag banh mi godard. Cronut lyft actually iceland, pabst chia small batch fam. Aesthetic organic ennui stumptown. </span></p>
        </div>
        <div class="column-content-link">
                <a href="https://www.google.com">Learn more</a>
        </div>
</div>

<div class="column-content">
        <div class="column-content-header">
                <i class="icon fa fa-rocket"></i>
            <h3 class="">Heading 3</h3>
            <p><span>Blue bottle heirloom cronut, iceland salvia austin swag af photo booth church-key vinyl yuccie pour-over venmo bitters. Drinking vinegar humblebrag banh mi godard. Cronut lyft actually iceland, pabst chia small batch fam. Aesthetic organic ennui stumptown. </span></p>
        </div>
        <div class="column-content-link">
                <a href="https://www.google.com">Learn more</a>
        </div>
</div>

<div class="column-content">
        <div class="column-content-header">
                <i class="icon fa fa-rocket"></i>
            <h3 class="">Heading 4</h3>
            <p><span>Blue bottle heirloom cronut, iceland salvia austin swag af photo booth church-key vinyl yuccie pour-over venmo bitters. Drinking vinegar humblebrag banh mi godard. Cronut lyft actually iceland, pabst chia small batch fam. Aesthetic organic ennui stumptown. </span></p>
        </div>
        <div class="column-content-link">
                <a href="https://www.google.com">Learn more</a>
        </div>
</div>

最佳答案

遍历到最近的父元素(.column-content),然后在其中找到目标元素:

$('.column-content-link a').hover(function(){
  var $closestColContent = $(this).closest('.column-content');
  closestColContent.find('.icon,h3').toggleClass('hover');
});

关于javascript - 将类添加到单个元素而不是全部在悬停时使用 JQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43674916/

相关文章:

javascript - 如何使用 github API 从网络浏览器编辑 github 文件?

html - 用于打印的 3 个文本 div 的最佳布局

javascript - 如何绑定(bind)到表 <tr>

javascript - jQuery:如何隐藏 div 中除 2 个特定元素之外的所有内容

javascript - 扩展 typescriptdefineTyped 定义文件

javascript - javascript回调函数在哪个线程执行?

javascript - 加载完成时模态关闭

javascript - 表单提交后 Magento 电子邮件成功消息返回空白页

jquery error() 调用显示在 firebug 配置文件中

javascript - 内联 SVG 无法在 Angular Material 对话框中正确缩放