开发者

Make hover over one class affect all other of same class on page

开发者 https://www.devze.com 2023-04-02 08:53 出处:网络
I\'m looking to make a hover over list items in a sub nav activate a class on all of the items in that category on the page (not just parent or sibling elements).Any ideas?Here\'s an example of what I

I'm looking to make a hover over list items in a sub nav activate a class on all of the items in that category on the page (not just parent or sibling elements). Any ideas? Here's an example of what I mean:

<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>

&l开发者_JAVA百科t;!--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">


this should do what you want..

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

and

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


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.
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号