javascript - In jQuery how to select all other parent li of not clicked anchor? -
i have following simple nav links , want make sure when click on of links, "active" class removed other li except 1 clicked. in jquery how can setup selector achieve this?
<ul class="nav"> <li><a href="#">link 1</a></li> <li class="active"><a href="#">link 2</a></li> <li class="active"><a href="#">link 3</a></li> <li><a href="#">link 4</a></li> </ul>
this take on this...
$(function() { $('.nav li a').on('click', function() { $('.nav li').removeclass('active'); // remove active class $(this).closest('li').addclass('active'); // add active classs element clicked }); });
.active { color: red; border: 1px solid green; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="nav"> <li><a href="#">link 1</a> </li> <li class="active"><a href="#">link 2</a> </li> <li class="active"><a href="#">link 3</a> </li> <li><a href="#">link 4</a> </li> </ul>
Comments
Post a Comment