我正在使用两个按钮和一些jQuery来切换(展开/折叠)其中包含表格的两个div。当我用按钮1切换第一个div时,按钮2被推到div下方。我怎么能调整我的代码,以便按钮保持在相同的位置,只需切换下面的div。
这是一个codepen:
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而不是我想要的内容。
答案 0 :(得分:0)
我做了一些CSS更改,以保持按钮的位置。看起来很有效:
button {
position: absolute;
top: 0;
}
#btnNoMatch {
left: 100px;
}
.accordion-content {
margin-top: 40px;
}
答案 1 :(得分:0)
这是一个简单的例子。 http://jsfiddle.net/et8mLa0c/
<强> HTML 强>
<div id="outer_wrapper">
<div id="buttons_container">
<a href="javascript:" data-table="table_one" class="toggle_button">Open Table One</a>
|
<a href="javascript:" data-table="table_two" class="toggle_button">Open Table Two</a>
</div>
<table id="table_one">
<tr>
<td>Table One</td>
</tr>
<tr>
<td>Table One</td>
</tr>
<tr>
<td>Table One</td>
</tr>
</table>
<table id="table_two">
<tr>
<td>Table Two</td>
</tr>
<tr>
<td>Table Two</td>
</tr>
<tr>
<td>Table Two</td>
</tr>
</table>
</div>
<强> CSS 强>
#buttons_container{
height:50px;
position:relative;
}
a.toggle_button{
text-decoration:none;
}
table{
left:10px;
top:50px;
width:350px;
display:none;
position:absolute;
border:solid 1px #ddd;
border-collapse:collapse;
}
table td{
border:solid 1px #ddd;
}
<强>的Javascript 强>
$(document).ready(function(){
var $tables = $('table'),
$btns = $('.toggle_button');
$btns.click(toggleTable);
function toggleTable(event){
event.preventDefault();
var $target = $(event.target);
var tableId = $target.data("table");
$tables.hide();
$('#' + tableId).show();
}
});
答案 2 :(得分:0)
如果使用索引将按钮映射到accordion-content div,则可以将按钮保存在HTML中。
<强> JS 强>
$("#accordion").find(".accordion-toggle").click(function () {
var selectedAccordion = $(".accordion-content").eq($(this).index());
selectedAccordion.slideToggle("fast");
$(".accordion-content").not(selectedAccordion).slideUp("fast");
});
<强> HTML 强>
<div id="accordion">
<button type="button" class="btn btn-success accordion-toggle" id="btnMatch">
Matched</button>
<button type="button" class="btn btn-danger accordion-toggle" id="btnNoMatch">
No Match</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>
<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>