简体   繁体   中英

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

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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