gpt4 book ai didi

javascript - 将事件类添加到多级 Accordion

转载 作者:太空宇宙 更新时间:2023-11-04 15:48:22 25 4
gpt4 key购买 nike

我有一个多级 Accordion ,但只缺少事件类,只针对事件/打开面板,没有改变任何其他东西,有什么帮助吗?

JS:

$('.toggle').click(function(e) {
e.preventDefault();

var $this = $(this);

if ($this.next().hasClass('show')) {
$this.next().removeClass('show');
$this.next().slideUp(350);
} else {
$this.parent().parent().find('li .inner').removeClass('show');
$this.parent().parent().find('li .inner').slideUp(350);
$this.next().toggleClass('show');
$this.next().slideToggle(350);}
});

这是 CodePen:https://codepen.io/mozes22/pen/XxQEBp

最佳答案

请检查此代码。

<style>
ul
{list-style: none;
padding: 0;}

ul .inner
{padding-left: 1em;
overflow: hidden;
display: none;}

ul .inner.show {
/*display: block;*/}

ul li
{margin: 0.5em 0;}

ul li a.toggle
{width: 100%;
display: block;
background: grey;
color: #fefefe;
padding: 0.75em;
border-radius: 0.15em;
transition: background 0.3s ease;
text-decoration: none;}

ul li a.toggle:after
{content: '\002B';
float: right;}

ul li a.toggle:hover {
background: yellow;}

.modal-dialog {
max-width: 70% !important;
margin: auto;}

.modal-content
{height: 500px; }

.modal-body
{background-color: rgb(3, 119, 184);
height: 250px;
overflow-y: scroll;}
</style>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script>


<ul class="accordion">
<li>
<a class="toggle" href="javascript:void(0);"><b>- Technical for Sales People</b></a>
<ul class="inner">test</ul>
</li>
<li>
<a class="toggle" href="javascript:void(0);"><b>- New to Car Sales Foundation</b></a>
<ul class="inner">test2</ul>
</li>
</ul>

<script>
$(document).ready(function() {
$(".inner").hide();
$(".inner:first").show();
$(".toggle").click(function(){
if ($(this).is(".show"))
{
$(this).removeClass("show");
$(this).next(".inner").slideUp(400);
}
else
{
$(".inner").slideUp(400);
$(".toggle").removeClass("show");

$(this).addClass("current");
$(this).next(".inner").slideDown(400);
}
});
});
</script>

关于javascript - 将事件类添加到多级 Accordion ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53049222/

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