使用jQuery单击链接时更改链接背景颜色

时间:2022-11-21 21:11:31

I have a basic jQuery Accordion. I'd like to change the background colour on a link if that link is clicked.

我有一个基本的jQuery手风琴。如果点击该链接,我想更改链接上的背景颜色。

So if I click 'Dropdown 1', the background colour changes to red. How do I do this?

因此,如果我单击“下拉1”,背景颜色将变为红色。我该怎么做呢?

Demo: http://jsfiddle.net/4dm318nn/

  $(document).ready(function($) {
    $('.inline').find('.navtoggle').click(function(){

      //Expand or collapse this panel
      $(this).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>
	</ul>
</nav>

2 个解决方案

#1


2  

Add red color when clicked

单击时添加红色

CSS

  .accordion-toggle {cursor: pointer;}
  .sub-menu {display: none;}
  .sub-menu .sub-menu {display: block;}

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/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>
    </ul>
</nav>

Javascript

$(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');

});
});

#2


0  

not sure this is what you want, but is

不确定这是你想要的,但是

$(".inline.naked li").click(function(color){
   var bgColor = typeof x === "undefined" ? "red" : color;
   $(this).css("background-color", color);
});

what you want?

你想要什么?

#1


2  

Add red color when clicked

单击时添加红色

CSS

  .accordion-toggle {cursor: pointer;}
  .sub-menu {display: none;}
  .sub-menu .sub-menu {display: block;}

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/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>
    </ul>
</nav>

Javascript

$(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');

});
});

#2


0  

not sure this is what you want, but is

不确定这是你想要的,但是

$(".inline.naked li").click(function(color){
   var bgColor = typeof x === "undefined" ? "red" : color;
   $(this).css("background-color", color);
});

what you want?

你想要什么?