简体   繁体   中英

toggle css class for active link

I have 5 links on page, when i click on each link it will change the color but as soon as ma=ove my cursor and place on other area of page, the focus on active link gets disappear. so I am not able to show the current link state. please tell me how to fix

Html

<tr>
 <td>
  <a href="#" class="linkbold">Basic2</a>
 </td>
</tr>
<tr>
 <td>
  <a href="#" class="linkbold">Basic3</a>
 </td>
</tr>
<tr>
 <td>
  <a href="#" class="linkbold">Basic4</a>
 </td>
</tr>

Css

.linkbold{ color:#0066FF; font-family:Tahoma; font-size:11px; text-decoration:none; font-weight:bold !important; }
.active { color: red; } // define any color for active class

// you can use css shorthand for font property
.linkbold { font: bold 11px Tahoma, sans-serif; color:#0066FF; }

If you want to know more about css shorthand

Jquery

$('a.linkbold').on('click', function(){
    $('a.linkbold').removeClass('active'); // remove any active class
    $(this).addClass('active'); // and then add active class for clicked element.
});​

Working example here

<html>
<head>
    <style type="text/css">
        a:link {color:blue;}
        a:visited {color:red;}
        a:hover {color:green;}
    </style>
</head>
<body>
    <a href="#">first</a>
    <br/>
    <a href="#">second</a>
</body>
</html>

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