【发布时间】:2015-08-04 03:25:44
【问题描述】:
我正在使用两个按钮和一些 jQuery 来切换(展开/折叠)两个带有表格的 div。当我用按钮 1 切换第一个 div 时,按钮 2 被按下 div 下方。我怎样才能调整我的代码,使按钮保持在相同的位置,只需切换下面的 div。
这是一个代码笔:
http://codepen.io/anon/pen/EjGrKX
HTML
<div id="accordion">
<button type="button" class="btn btn-success accordion-toggle" id="btnMatch">
Matched</button>
<div class="accordion-content">
<table class="table" id="match-table">
<thead>
<tr>
<th>Test</th>
<th>Test</th>
<th>Test</th>
<th>Test</th>
</tr>
</thead>
<tbody>
<tr>
<td>
Test
</td>
<td>
Test
</td>
<td>
Test
</td>
<td>
Test
</td>
</tr>
</tbody>
</table>
</div>
<button type="button" class="btn btn-danger accordion-toggle" id="btnNoMatch">
No Match</button>
<div class="accordion-content">
<table class="table" id="no-match-table">
<thead>
<tr>
<th>Test</th>
<th>Test</th>
<th>Test</th>
<th>Test</th>
</tr>
</thead>
<tbody>
<tr>
<td>
Test
</td>
<td>
Test
</td>
<td>
Test
</td>
<td>
Test
</td>
</tr>
</tbody>
</table>
</div>
</div>
CSS
#btnMatch {
margin-right: 10px;
}
.accordion-toggle {cursor: pointer;}
.accordion-content {display: none;}
JS
$(document).ready(function ($) {
$("#accordion").find(".accordion-toggle").click(function () {
$(this).next().slideToggle("fast");
$(".accordion-content").not($(this).next()).slideUp("fast");
});
});
如果我只是将按钮放在一起,那么按钮 1 将切换按钮 2 而不是我想要的内容。
【问题讨论】:
标签: javascript jquery html css twitter-bootstrap