【问题标题】:on link hover add/remove css class to image在链接悬停上添加/删除 css 类到图像
【发布时间】:2013-08-20 11:35:30
【问题描述】:

我正在尝试添加和删除类,将链接悬停在其上方的图像上(在同一个容器内),但我遗漏了一些东西,因为它不起作用:

这是 jQuery 中的 sn-p:

jQuery('.category-view a').hover(
  function () {
    jQuery(this).parent('img').removeClass('nzi');
  },
  function () {

    jQuery(this).parent('img').addClass('nzi');
  }

这是 HTML:

<div class="category-view">
    <div class="row">
        <div class="category floatleft width33 vertical-separator " style="margin:50px;">
            <div class="spacer">
                <img src="/harbini/images/stories/virtuemart/category/resized/lips_202x296.png" alt="lips">                     
                <h2 class="catSub"> <a href="/harbini/index.php/al/produktet/grim/buzet" title="Buzët"> Buzët </a> </h2>

                <a href="/harbini/index.php/al/produktet/grim/buzet" class="category-overlay"><span></span></a>
            </div>
        </div>

        <div class="category floatleft width33 " style="margin:50px;">
            <div class="spacer">
                <img src="/harbini/images/stories/virtuemart/category/resized/face_202x296.png" alt="face.png">                     
                <h2 class="catSub"> <a href="/harbini/index.php/al/produktet/grim/fytyra" title="Fytyra"> Fytyra </a> </h2>

                <a href="/harbini/index.php/al/produktet/grim/fytyra" class="category-overlay"><span></span></a>
            </div>
        </div>
        <div class="clear"></div>
    </div>

    <div class="horizontal-separator"></div>

    <div class="row">
        <div class="category floatleft width33 vertical-separator " style="margin:50px;">
            <div class="spacer">
                <img src="/harbini/images/stories/virtuemart/category/resized/eyes_202x296.png" alt="eyes">                     
                <h2 class="catSub"> <a href="/harbini/index.php/al/produktet/grim/syte" title="Sytë"> Sytë </a> </h2>

                <a href="/harbini/index.php/al/produktet/grim/syte" class="category-overlay"><span></span></a>
            </div>
        </div>

        <div class="category floatleft width33  " style="margin:50px;">
            <div class="spacer">
                <img src="/harbini/images/stories/virtuemart/category/resized/nails_202x296.png" alt="nails.png">                       
                <h2 class="catSub"> <a href="/harbini/index.php/al/produktet/grim/thonjte" title="Thonjtë"> Thonjtë </a> </h2>

                <a href="/harbini/index.php/al/produktet/grim/thonjte" class="category-overlay"><span></span></a>
            </div>
        </div>
        <div class="clear"></div>
    </div>
</div>

【问题讨论】:

  • 图片不是主播的父母,他们是兄弟姐妹。
  • 请创建一个小提琴,以便更好地视觉理解
  • 我已经检查了你有两个锚链接的代码,一个是子链接,另一个是图像的兄弟,所以让我们知道你想在哪个锚链接上运行函数

标签: jquery


【解决方案1】:

这里img不是a元素的父元素,是兄弟元素,所以使用.siblings()

jQuery('.category-view a').hover(
  function () {
    jQuery(this).siblings('img').removeClass('nzi');
  },
  function () {
    jQuery(this).siblings('img').addClass('nzi');
  }

但是由于有两个a 元素(一个在h2 内)

jQuery('.category-view a').hover(
    function () {
        jQuery(this).closest('.category').find('img').removeClass('nzi');
    },
    function () {
        jQuery(this).closest('.category').find('img').addClass('nzi');
    }
);

【讨论】:

  • 你没有看到它有两个锚标签,一个是孩子,另一个是兄弟。
  • @TheMechanic 帖子我编辑了答案......如果反对者可以发表评论说明遗漏了什么以便可以纠正,这将对用户有所帮助
猜你喜欢
  • 1970-01-01
  • 2013-10-16
  • 1970-01-01
  • 1970-01-01
  • 2013-02-09
  • 2020-07-22
  • 1970-01-01
  • 2012-06-01
  • 2011-10-10
相关资源
最近更新 更多