Trying to use jQuery.addClass method with navigation

I have a Nav in which I am trying to use the jQuery addClass method to set the link color of the last link. The problem is that I have to use removeClass for all other links in Nav. this is what i have problems with.

I wrote the code in a naive way, but I know that this is not good programming. Below is the code with ref style sheet.

jQuery('#shop-nav').click(function(){
    jQuery("#shop-nav").addClass("clicked");
    jQuery("#art-nav").removeClass("clicked");
    jQuery("#obj-nav").removeClass("clicked");
    jQuery("#acc-nav").removeClass("clicked");
});

jQuery('#art-nav').click(function(){
    jQuery("#art-nav").addClass("clicked");
    jQuery("#shop-nav").removeClass("clicked");
    jQuery("#obj-nav").removeClass("clicked");
    jQuery("#acc-nav").removeClass("clicked");
});

etc .. etc!

HTML

<div id="nav-cell-1" class="grid f-cell nav-cell">
<ul id="main-nav" class="nav clearfix">
    <li><a href="#" id="shop-nav">Shop</a>
        <ul id="shop-cats">
            <li><a href="#" id="art-nav">Art</a></li>
            <li>&#8226;</li>
            <li><a href="#" id="obj-nav">Objects</a></li>
            <li>&#8226;</li>
            <li><a href="#" id="acc-nav">Accessories</a></li>
        </ul>
    </li>
</ul>
</div>

CSS

a:link, a:visited {color:#cfb199;text-decoration:none} /* official this color:#9d9fa1; work color: #222*/
a:active, a:hover {color:#9d9fa1;text-decoration:none} /* old color:#9d9fa1; */ /* official color:#cfb199; work color: #f00*/
a:link.clicked, a:visited.clicked {color:green;text-decoration:underline}

demo site is here: http://www.tomcarden.net/birdycitynav/partial-nav-demo.html

I solved part of the problem using the link this, but this does not include the part .removeClass.

jQuery('#shop-cats>li>a').click(function(){
    jQuery(this).addClass("clicked");
});
+3
source share
5 answers

Or it looks more like your site:

$('.nav a').click(function(){
    $('.nav a').removeClass('clicked');
    $(this).toggleClass('clicked');
});

: http://www.jsfiddle.net/mjYq3/18/

+3

, :

var navs = jQuery('#shop-nav,#art-nav, #obj-nav, #acc-nav');
navs.click(function(){
    navs.removeClass("clicked");
    $(this).addClass("clicked");
});
0

jQuery('#shop-cats>li>a').click(function(){
    $this = jQuery(this);
    $this.parent().children('a').removeClass('clicked');
    $this.addClass("clicked");
});
0

You can delete all the clicked classes first and then add them back to the one that was clicked.

jQuery('#shop-cats>li>a').click(function(){
  jQuery('#shop-cats>li>a').removeClass("clicked")
  jQuery(this).addClass("clicked");
});
0
source

This should work:

$('#main-nav a').click(function() {
    $('#main-nav a').not(this).removeClass('clicked');
    $(this).addClass('clicked');
});

Based on the fact that you apparently want to do this for all links that are descendants #main-nav, and not just those in the internal list <ul>.

0
source

Source: https://habr.com/ru/post/1791327/


All Articles