【发布时间】:2019-01-23 19:30:55
【问题描述】:
我已经使用 jquery 完成了上滑和下滑 div。这里我有三个盒子。一旦我们点击头部,内容就会打开。然后单击另一个头,该内容将打开并关闭旧内容。但我继续关闭相同的内容。任何时候任何内容都是开放的。我需要一旦我们点击 head 内容就需要关闭。
$(document).ready(function() {
$("p").click(function() {
$this = $(this);
var parent = $this.closest('.acc');
$("p").removeClass('open');
$this.addClass('open');
$('.acc-body').slideUp();
parent.find('.acc-body').slideDown();
});
});
body {
padding: 0;
margin: 0;
}
*,
*:before,
*:after {
box-sizing: border-box;
}
.acc {
padding: 0px;
margin: 0;
}
.acc-head {
padding: 15px;
margin: 0;
background: #ccc;
}
.acc-head.open {
background: #000;
color: #fff;
}
.acc-body {
border: 1px solid #ccc;
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div style="max-width: 500px;">
<div class="acc">
<p class="acc-head">
head
</p>
<div class="acc-body">
Learn by examples! At W3Schools you will find a lot of jQuery examples to edit and test yourself.
</div>
</div>
<div class="acc">
<p class="acc-head">
head
</p>
<div class="acc-body">
Learn by examples! At W3Schools you will find a lot of jQuery examples to edit and test yourself.
</div>
</div>
<div class="acc">
<p class="acc-head">
head
</p>
<div class="acc-body">
Learn by examples! At W3Schools you will find a lot of jQuery examples to edit and test yourself.
</div>
</div>
</div>
【问题讨论】:
标签: javascript jquery