展开和折叠功能目前工作正常,但我想修改该功能,因此当我单击 Expand 1
,然后单击 Expand 2
时, Expand 1
应该会自动折叠。这意味着一次只允许展开一个 div
,而所有其他的都应保持折叠状态。
HTML:
<div class="container">
<div class="header"><span>Expand 1</span>
</div>
<div class="content">
<ul>
<li>This is just some random content.</li>
<li>This is just some random content.</li>
<li>This is just some random content.</li>
<li>This is just some random content.</li>
</ul>
</div>
<div class="header"><span>Expand 2</span>
</div>
<div class="content">
<ul>
<li>This is just some random content.</li>
<li>This is just some random content.</li>
<li>This is just some random content.</li>
<li>This is just some random content.</li>
</ul>
</div>
CSS
.container {
width:100%;
border:1px solid #d3d3d3;
}
.container div {
width:100%;
}
.container .header {
background-color:#d3d3d3;
padding: 2px;
cursor: pointer;
font-weight: bold;
}
.container .content {
display: none;
padding : 5px;
}
查询
$(".header").click(function () {
$header = $(this);
//getting the next element
$content = $header.next();
//open up the content needed - toggle the slide- if visible, slide up, if not slidedown.
$content.slideToggle(500, function () {
//execute this after slideToggle is done
//change text of header based on visibility of content div
$header.text(function () {
//change text based on condition
return $content.is(":visible") ? "Collapse" : "Expand";
});
});
});
例子..
最佳答案
你可以做到,
$(".header").click(function () {
$header = $(this);
$content = $header.next();
$(".content").not($content).slideUp().prev().text("Expand");
$content.slideToggle(500, function () {
$header.text(function () {
return $content.is(":visible") ? "Collapse" : "Expand";
});
});
});
- 折叠所有,除了当前的 使用代码
$(".content").not($content).slideUp().prev().text("Expand");
关于javascript - 展开和折叠 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33951123/