gpt4 book ai didi

javascript - 通过 jquery 协助对嵌套 html 表进行分组

转载 作者:行者123 更新时间:2023-11-27 22:56:21 25 4
gpt4 key购买 nike

基本上我有一个带有 3 层展开/折叠功能的 html 表格。我想要实现的目标是让这些层实际上在 parent 下方展开/折叠,而不是向右,基本上一旦展开,它们就会变成一个向下的斜坡。这是我的 JSFiddle:

https://jsfiddle.net/htfLmekL/1/

//expand collapse based on parent class which is column 1
$(document).ready(function() {
$('.parent').prepend('-');

$('.parent').on('click', function() {
if ($(this).text().indexOf('-') != -1) {
var str0 = $(this).text().replace(/-/g, '+');
$(this).text(str0);
} else {
var str = $(this).text().replace(/\+/g, '-');
$(this).text(str);
}
var $row = $(this).parent();
var rowspan = +$(this).attr('rowspan') || 4;
$.merge($row, $row.nextAll()).slice(0, rowspan).find('.alliance').toggle();
$.merge($row, $row.nextAll()).slice(0, rowspan).find('.race').toggle();
$.merge($row, $row.nextAll()).slice(0, rowspan).find('.role').toggle();
$.merge($row, $row.nextAll()).slice(0, rowspan).find('.resource').toggle();
$.merge($row, $row.nextAll()).slice(0, rowspan).find('.weapon').toggle();
$.merge($row, $row.nextAll()).slice(0, rowspan).find('.price').toggle();

});
});

最佳答案

创建了这样的东西......尽管你必须大量调整 CSS 才能使其完美。希望这有帮助...

        <style>
.tftable {
font-size: 12px;
color: #333333;
width: 100%;
border-width: 1px;
border-color: #729ea5;
border-collapse: collapse;
}

.tftable th {
font-size: 12px;
background-color: #acc8cc;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #729ea5;
text-align: left;
}

.tftable tr {
background-color: #d4e3e5;
}

.tftable td {
font-size: 12px;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #729ea5;

}

.tftable tr:hover {
background-color: #ffffff;
}

.race {
width: 50px;
}


</style>

</head>
<body>








<table class="tftable" border="1">
<tr>
<th>Group</th>
</tr>
<tr>
<th><a onclick="javascript:toggleDiv(this,'grp1child1');"> - </a>Group 1

<table id="grp1child1" class="tftable" border="1">
<tr>
<th>Alliance : <a onclick="javascript:toggleDiv(this,'grp1child2');"> - </a>Good</th>
</tr>
<tr>
<td >
<table id="grp1child2" class="tftable" border="1">
<tr>
<th class="race">Race: </th>
<th><a onclick="javascript:toggleDiv(this,'grp1child3');" > - </a>Humans</th>
<th><a onclick="javascript:toggleDiv(this,'grp1child4');"> - </a>Elves</th>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<table id="grp1child3" class="tftable" border="1">
<tr>
<th colspan="3">&nbsp;</th>
<th>Price</th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>

</table>

</td>
<td>
<table id="grp1child4" class="tftable" border="1">
<tr>
<th colspan="3">&nbsp;</th>
<th>Price</th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>

</table>
</td>
</tr>

</table>
</td>
</tr>



</table>

</th>
</tr>
<tr>
<th><a onclick="javascript:toggleDiv(this,'grp2child1');"> - </a>Group 2

<table id="grp2child1" class="tftable" border="1">
<tr>
<th >Alliance : <a onclick="javascript:toggleDiv(this,'grp2child2');"> - </a>Evil</th>
</tr>
<tr>
<td>
<table id="grp2child2" style="width : 100%">
<tr>
<th class="race">Race: </th>
<th><a onclick="javascript:toggleDiv(this,'grp2child3');" > - </a>Trolls</th>
<th><a onclick="javascript:toggleDiv(this,'grp2child4');"> - </a>Ogres</th>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<table id="grp2child3" class="tftable" border="1">
<tr>
<th colspan="3">&nbsp;</th>
<th>Price</th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>

</table>

</td>
<td>
<table id="grp2child4" class="tftable" border="1">
<tr>
<th colspan="3">&nbsp;</th>
<th>Price</th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>

</table>
</td>
</tr>

</table>
</td>
</tr>

</table>

</th>
</tr>




</table>



<script>

function toggleDiv(linkobj,divId) {
linkobj.innerHTML = (linkobj.innerHTML === ' - ') ? ' + ' : ' - ' ;
$("#"+divId).toggle();
}


</script>

关于javascript - 通过 jquery 协助对嵌套 html 表进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37581062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com