【发布时间】:2018-06-04 15:26:12
【问题描述】:
我从这个网站上已有的答案中得到了以下代码。使用以下 JQuery 代码,表格行默认折叠,然后通过单击“标题 id”我可以展开表格。但是,如果我再次单击标题 ID,则行不会折叠。我需要在这段代码中添加一些东西来让它做到这一点。如果可能的话,我还想添加用户可以单击的 + 和 - 图标,而不是标题 ID 本身。有人可以帮忙吗?
<!DOCTYPE html>
<head>
<title>Master Vocab List</title>
<script type="text/javascript" src="jquery.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
$("#collapse").hide();
$("#collapse1").hide();
$("#collapse2").hide();
$("#collapse3").hide();
$("#collapse4").hide();
$("#collapse5").hide();
$("#collapse6").hide();
$("#collapse7").hide();
$("#collapse8").hide();
});
$("#heading").click(function(){
$("#collapse").show();
$("#collapse1").show();
$("#collapse2").show();
$("#collapse3").show();
$("#collapse4").show();
$("#collapse5").show();
$("#collapse6").show();
$("#collapse7").show();
$("#collapse8").show();
});
</script>
<table>
<tr>
<td id="heading" colspan = "2"><b>Connectors and
Transitions</b></td>
</tr>
<tr id="collapse">
<td>primero</td>
<td>first</td>
</tr>
<tr id="collapse1">
<td>después</td>
<td>afterwards</td>
</tr>
<tr id="collapse2">
<td>pero</td>
<td>but</td>
</tr>
<tr id="collapse3">
<td>y</td>
<td>and</td>
</tr>
<tr id="collapse4">
<td>luego</td>
<td>afterwards</td>
</tr>
<tr id="collapse5">
<td>antes</td>
<td>beforehand</td>
</tr>
<tr id="collapse6">
<td>finalmente</td>
<td>finally</td>
</tr>
<tr id="collapse7">
<td>por ejemplo</td>
<td>for example</td>
</tr>
<tr id="collapse8">
<td>mientras</td>
<td>while</td>
</tr>
</table>`
【问题讨论】:
-
您可以添加图像或字体真棒图标并将 Id 属性从标题移动到这个新元素以完成 +。然后不要在代码中调用 .show do .toggle
-
感谢您提供的信息。效果很好!
标签: jquery expandable collapsable