【问题标题】:Make hover over one class affect all other of same class on page将鼠标悬停在一个班级上会影响页面上所有其他相同班级
【发布时间】:2011-11-10 21:21:36
【问题描述】:

我希望将鼠标悬停在子导航中的列表项上,激活页面上该类别中所有项目的类(不仅仅是父元素或兄弟元素)。有任何想法吗?这是我的意思的一个例子:

<style>
img.BLUE {border:1px solid #FFF}
</style>

<ul id="subnav">
<li id="BLUE">Blue</li> <!--When hovering these...-->
<li id="PINK">Pink</li>
<li id="YELLOW">Yellow</li>
</ul>

<!--other stuff here-->

<img class="BLUE" href="image.jpg"> <!--it applies the border to this and any other img.blue on the page-->
<img class="PINK" href="image1.jpg">
<img class="YELLOW" href="image2.jpg">

【问题讨论】:

    标签: javascript jquery css hover


    【解决方案1】:

    DEMO

    $('#subnav li').hover(function(){
        var id = $(this).attr('id');                     // grab ID of clicked el
        $('img.'+id).css({border: '1px solid #fff'});    // image class=IDname : add CSS prop.
    });
    

    【讨论】:

    • 这不能回答问题。 OP 特别要求 hover 不点击。
    【解决方案2】:

    这应该做你想做的..

    <style type="text/css">
       img.active{border:1px solid #FFF;}
    </style>
    

    $('#subnav li').hover(function(){
      $('.' + this.id).addClass('active');
    },function(){
      $('.' + this.id).removeClass('active');
    });
    

    【讨论】:

    • 感谢您的回答,但我在执行脚本时遇到了麻烦。我可能会用 class/id 配对来混淆它。可以显示html的一面吗?
    猜你喜欢
    • 2016-06-06
    • 2016-11-25
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多