gpt4 book ai didi

javascript - 如果其中一个子菜单项处于事件状态,则展开下拉菜单

转载 作者:行者123 更新时间:2023-11-30 13:16:49 25 4
gpt4 key购买 nike

我有一个下拉菜单,可以通过单击展开。默认情况下,菜单是折叠的。如果其中一个子菜单页面处于事件状态,我希望菜单看起来是展开的。我希望这很清楚。这是我的代码。

HTML

<li class="dropdown">
<a class="disablelink" href="#">Carbohydrates, proteins &amp; fats</a>
<ul class="sub_navigation">
<li><a href="carbohydrates.php">Carbohydrates</a></li>
<li><a href="proteins.php">Proteins</a></li>
<li><a href="fats.php">Fats</a></li>
</ul>
</li>

jQuery

$(document).ready(function() {
$('.dropdown').click(function() {
// When the event is triggered, grab the current element 'this' and
// find it's children '.sub_navigation' and display/hide them
$(this).find('.sub_navigation').slideToggle();
});

$(".disablelink").click(function(e) {
e.preventDefault();
});

});

因此,如果用户在 carbohydrates.phpproteins.phpfats.php 上,我希望扩展菜单。我不知道该怎么做。谁能帮忙?

最佳答案

例如,当一个页面处于事件状态时,您需要为其链接提供一个额外的类;

<li class="current_page"><a href="carbohydrates.php">Carbohydrates</a></li> .

然后您可以使用 jQuery 遍历菜单并在任何菜单项具有该类时展开它。

$('.dropdown li').each(function() {
if ($(this).hasClass('current_page')) {
$(this).closest('.sub_navigation').slideDown();
}
});

演示:http://jsfiddle.net/R7gkw/

关于javascript - 如果其中一个子菜单项处于事件状态,则展开下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11722494/

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