gpt4 book ai didi

javascript - 显示下拉内容

转载 作者:行者123 更新时间:2023-11-30 15:52:02 25 4
gpt4 key购买 nike

我的代码在单击时展开下拉列表以显示一组链接。共有三个不同的下拉菜单,每个下拉菜单都有一组不同的链接,当用户点击时会显示这些链接。

脚本在下面,但它不是动态的。什么是更好、更有效的脚本编写方式,以便在需要额外的下拉列表时无需添加?

$(".button-nav1").click(function() {
$(".row1").toggle();
});

$(".button-nav2").click(function() {
$(".row2").toggle();
});

$(".button-nav3").click(function() {
$(".row3").toggle();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="row1" class="button-nav1">Joe<span class="crt">&nbsp;▼</span>
</div>
<div class="row1 BGpadding">
<ul class="2ndRESET">
<li class="resetTWO"><strong>Overview</strong>
</li>
<li><strong>Training</strong>
</li>
<ul class="resetTWO">
<li><a href="#">Lorem Lipsum</a>
</li>
</ul>
<li><strong>Toomy</strong>
</li>
<ul class="resetTWO">
<li><a href="#">Lipsum </a>
</li>
<li><a href="#">Loremu</a>
</li>
<li><a href="#">Lipsum</a>
</li>
</ul>
</ul>
</div>
</div>

<div id="row2" class="button-nav2">Joe<span class="crt">&nbsp;▼</span>
</div>
<div class="row2 BGpadding">
<ul class="2ndRESET">
<li class="resetTWO"><strong>Overview</strong>
</li>
<li><strong>Training and Help</strong>
</li>
<ul class="resetTWO">
<li><a href="#">Lorem Lipsum</a>
</li>
</ul>
<li><strong>Toomy</strong>
</li>
<ul class="resetTWO">
<li><a href="#">Lipsum </a>
</li>
<li><a href="#">Loremu</a>
</li>
<li><a href="#">Lipsum</a>
</li>
</ul>
</ul>
</div>
</div>

<div id="row3" class="button-nav3">Joe<span class="crt">&nbsp;▼</span>
</div>
<div class="row3 BGpadding">
<ul class="2ndRESET">
<li class="resetTWO"><strong>Overview</strong>
</li>
<li><strong>Training</strong>
</li>
<ul class="resetTWO">
<li><a href="#">Lorem Lipsum</a>
</li>
</ul>
<li><strong>Toomy</strong>
</li>
<ul class="resetTWO">
<li><a href="#">Lipsum </a>
</li>
<li><a href="#">Loremu</a>
</li>
<li><a href="#">Lipsum</a>
</li>
</ul>
</ul>
</div>
</div>

最佳答案

您可以将 jquery 缩减为一个函数。首先收听 divs,它有一个以 button-nav 开头的类和 $("div[class^=button-nav]")查询。然后获取 id 并使用 $("."+$(this).attr('id')).toggle( );

$("div[class^=button-nav]").on('click',function() {
$("."+$(this).attr('id')).toggle();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="row1" class="button-nav1">Joe<span class="crt">&nbsp;▼</span>
</div>
<div class="row1 BGpadding">
<ul class="2ndRESET">
<li class="resetTWO"><strong>Overview</strong>
</li>
<li><strong>Training</strong>
</li>
<ul class="resetTWO">
<li><a href="#">Lorem Lipsum</a>
</li>
</ul>
<li><strong>Toomy</strong>
</li>
<ul class="resetTWO">
<li><a href="#">Lipsum </a>
</li>
<li><a href="#">Loremu</a>
</li>
<li><a href="#">Lipsum</a>
</li>
</ul>
</ul>
</div>
</div>

<div id="row2" class="button-nav2">Joe<span class="crt">&nbsp;▼</span>
</div>
<div class="row2 BGpadding">
<ul class="2ndRESET">
<li class="resetTWO"><strong>Overview</strong>
</li>
<li><strong>Training and Help</strong>
</li>
<ul class="resetTWO">
<li><a href="#">Lorem Lipsum</a>
</li>
</ul>
<li><strong>Toomy</strong>
</li>
<ul class="resetTWO">
<li><a href="#">Lipsum </a>
</li>
<li><a href="#">Loremu</a>
</li>
<li><a href="#">Lipsum</a>
</li>
</ul>
</ul>
</div>
</div>

<div id="row3" class="button-nav3">Joe<span class="crt">&nbsp;▼</span>
</div>
<div class="row3 BGpadding">
<ul class="2ndRESET">
<li class="resetTWO"><strong>Overview</strong>
</li>
<li><strong>Training</strong>
</li>
<ul class="resetTWO">
<li><a href="#">Lorem Lipsum</a>
</li>
</ul>
<li><strong>Toomy</strong>
</li>
<ul class="resetTWO">
<li><a href="#">Lipsum </a>
</li>
<li><a href="#">Loremu</a>
</li>
<li><a href="#">Lipsum</a>
</li>
</ul>
</ul>
</div>
</div>

关于javascript - 显示下拉内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39171219/

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