简体   繁体   中英

Background colour on click - jQuery

Currently when I click a link it sets the background colour to red. And keeps it red. How do I only set it to red for an active link? So I don't have multiple red background links.

Demo: https://jsfiddle.net/4dm318nn/16/

  $(document).ready(function($) { $('.inline').find('.navtoggle').click(function(){ //Expand or collapse this panel $(this).css('background-color','red').next().slideToggle('fast'); //Hide the other panels $(".sub-menu").not($(this).next()).slideUp('fast'); }); }); 
  .accordion-toggle {cursor: pointer;} .sub-menu {display: none;} .sub-menu .sub-menu {display: block;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <nav class="medium-8 columns primary menu"> <ul class="inline naked"> <li class="primary-item"><a href="http://localhost:8888/test/wordpress">Home</a></li> <li id="menu-item-74" class="primary-item menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-74"><a href="#" class="navtoggle">Dropdown 1</a> <ul class="sub-menu"> <li id="menu-item-76" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-76"><a href="http://localhost:8888/test/wordpress/?page_id=45">Dropdown 2</a> <ul class="sub-menu"> <li id="menu-item-77" class="sub-sub-menu menu-item menu-item-type-post_type menu-item-object-page menu-item-77"><a href="http://localhost:8888/test/wordpress/?page_id=47">Test 1</a></li> <li id="menu-item-78" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-78"><a href="http://localhost:8888/test/wordpress/?page_id=49">Test 2</a></li> </ul> </li> </ul> </li> <li id="menu-item-89" class="primary-item menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-74"><a href="#" class="navtoggle">Dropdown 2</a> <ul class="sub-menu"> <li id="menu-item-90" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-76"><a href="http://localhost:8888/test/wordpress/?page_id=45">Dropdown 2</a> <ul class="sub-menu"> <li id="menu-item-77" class="sub-sub-menu menu-item menu-item-type-post_type menu-item-object-page menu-item-77"><a href="http://localhost:8888/test/wordpress/?page_id=47">Test 1</a></li> <li id="menu-item-78" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-78"><a href="http://localhost:8888/test/wordpress/?page_id=49">Test 2</a></li> </ul> </li> </ul> </li> </ul> </nav> 

Add one line before:

$('.navtoggle').css('background','none'); // make none before adding bgcolor
$(this).css('background-color','red').next().slideToggle('fast');

Fiddle

Give all your link elements a common class, then set the background color of all elements within that class before setting the clicked link to red.

$('.commonClass').css('background-color', 'none');
$(this).css('background-color', 'red').next().slideToggle('fast');
$(document).ready(function($) {
  $('.inline').find('.navtoggle').click(function(){
    $(".navtoggle").each(function () {
        $(this).css('background-color','')
    });
  //Expand or collapse this panel
    $(this).css('background-color','red').next().slideToggle('fast');

  //Hide the other panels
  $(".sub-menu").not($(this).next()).slideUp('fast');

  });
});

You can do this by adding a class on the active element.

  $(document).ready(function($) {
    $('.inline').find('.navtoggle').click(function(){
        $('.navtoggle').removeClass('active');
        $(this).addClass('active');
      //Expand or collapse this panel
        $(this).next().slideToggle('fast');

      //Hide the other panels
      $(".sub-menu").not($(this).next()).slideUp('fast');

    });
  });

Example: https://jsfiddle.net/skeurentjes/4dm318nn/18/

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