gpt4 book ai didi

javascript - 单击时的背景颜色 - jQuery

转载 作者:太空宇宙 更新时间:2023-11-03 23:20:01 25 4
gpt4 key购买 nike

目前,当我点击一个链接时,它会将背景颜色设置为红色。并保持红色。如何仅将 active 链接设置为红色?所以我没有多个红色背景链接。

演示: 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>

最佳答案

在前面加一行:

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

Fiddle

关于javascript - 单击时的背景颜色 - jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29074580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com