gpt4 book ai didi

javascript - 在展开和折叠表格时默认隐藏行

转载 作者:行者123 更新时间:2023-11-28 17:10:07 26 4
gpt4 key购买 nike

我是一个非常新手的用户,我很难找到有关创建 html 表格的信息,这些表格默认情况下会显示父行和隐藏子行。我已经设法使用我在网上搜索发现的一些脚本使其工作,但默认情况下会显示子行。我不了解 JQuery,如果没有必要,我宁愿不添加另一种语言。我对 Javascript 的了解非常基础,是从我在网上找到的东西自学的。我对 CSS 和 HTML 的了解不错,但远非完美。

这是我的:

function	toggle_visibility(tbid,lnkid) {
if (document.getElementsByTagName) {
var tables = document.getElementsByTagName('table');
for (var i = 0; i < tables.length; i++) {
if (tables[i].id == tbid){
var trs = tables[i].getElementsByTagName('tr');
for (var j = 2; j < trs.length; j+=1) {
trs[j].bgcolor = '#CCCCCC';
if(trs[j].style.display == 'none')
trs[j].style.display = '';
else
trs[j].style.display = 'none';
}
}
}
}
var x = document.getElementById(lnkid);
if (x.innerHTML == '[-] ')
x.innerHTML = '[+] ';
else
x.innerHTML = '[-] ';
}
a {
color: #ff0000;
}
#exco {
color: #ff0000;
text-decoration: none;
}
#tbl3RD {
width: 100%;
border: 1px solid #ff0000;
border-bottom-width: 0px;
cellspacing: 0px;
border-spacing: 0px;
}
#dark {
background-color: #242424;
}
#light {
background-color: #8C8C8C;
}
#td75 {
width: 75%;
}
#td25 {
width: 25%;
}
#title {
font-size: 110%;
color: #FFFFFF;
font-weight: bold;
}
#subtitle {
color: #242424;
font-weight: bold;
}
<table id="tbl3RD" name="tbl3RD">
<tr id="dark">
<td colspan="2"></td>
</tr>
<tr id="dark">
<td id="title">
Title of the table.
</td>
<td id="td75">
<a id="exco" href="javascript:toggle_visibility('tbl3RD','lnk3RD');">
<div align="right" id="lnk3RD" name="lnk3RD">[-] </div>
</a>
</td>
</tr>
<tr id="light">
<td id="subtitle" colspan="2">
Subtitle row that explains the content.
</td>
</tr>
<tr>
<td>
</td>
<td id="td75">
Main content of the table.
</td>
</tr>
</table>

如果有人能帮我弄清楚如何默认隐藏副标题和主要内容行,我将永远欠你的债。

谢谢。

最佳答案

我正在尝试的最终结果是将大约 15 个表彼此重叠,以创建可扩展和折叠的级联行(某种程度上)。我设法让它与 body 标签中的 onload 一起工作,但是在让它同时与多个函数一起工作时遇到了问题。我通过为每个表各自的函数创建一个新的父函数来解决这个问题,如下所示:

<!--
function toggle_visibility(tbid,lnkid) {
if (document.getElementsByTagName) {
var tables = document.getElementsByTagName('table');
for (var i = 0; i < tables.length; i++) {
if (tables[i].id == tbid){
var trs = tables[i].getElementsByTagName('tr');
for (var j = 2; j < trs.length; j+=1) {
trs[j].bgcolor = '#CCCCCC';
if(trs[j].style.display == 'none')
trs[j].style.display = '';
else
trs[j].style.display = 'none';
}
}
}
}
var x = document.getElementById(lnkid);
if (x.innerHTML == '[-] ')
x.innerHTML = '[+] ';
else
x.innerHTML = '[-] ';
}

function start() {
toggle_visibility('tbl3RD','lnk3RD');
toggle_visibility('tblW8','lnkW8');
}
//-->
a {
color: #ff0000;
}
#exco {
color: #ff0000;
text-decoration: none;
}
#tbl3RD, #tblW8 {
width: 100%;
border: 1px solid #ff0000;
border-bottom-width: 0px;
cellspacing: 0px;
border-spacing: 0px;
}
#dark {
background-color: #242424;
}
#light {
background-color: #8C8C8C;
}
#td75 {
width: 75%;
}
#td25 {
width: 25%;
}
#title {
font-size: 110%;
color: #FFFFFF;
font-weight: bold;
}
#subtitle {
color: #242424;
font-weight: bold;
}
<html>
<head>
<title>Test</title>
<link rel="stylesheet" type="text/css" href="test.css">
<script src="test.js"></script>
</head>
<body onload="start();">
<table id="tbl3RD" name="tbl3RD">
<tr id="dark">
<td colspan="2"></td>
</tr>
<tr id="dark">
<td id="title">
Title of the table.
</td>
<td id="td75">
<a id="exco" href="javascript:toggle_visibility('tbl3RD','lnk3RD');">
<div align="right" id="lnk3RD" name="lnk3RD">[-] </div>
</a>
</td>
</tr>
<tr id="light">
<td id="subtitle" colspan="2">
Subtitle row that explains the content.
</td>
</tr>
<tr>
<td>
</td>
<td id="td75">
Main content of the table.
</td>
</tr>
</table>
<table id="tblW8" name="tblW8">
<tr id="dark">
<td colspan="2"></td>
</tr>
<tr id="dark">
<td id="title">
Title of the table.
</td>
<td id="td75">
<a id="exco" href="javascript:toggle_visibility('tblW8','lnkW8');">
<div align="right" id="lnkW8" name="lnkW8">[-] </div>
</a>
</td>
</tr>
<tr id="light">
<td id="subtitle" colspan="2">
Subtitle row that explains the content.
</td>
</tr>
<tr>
<td>
</td>
<td id="td75">
Main content of the table.
</td>
</tr>
</table>
</body>
</html>

关于javascript - 在展开和折叠表格时默认隐藏行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29403168/

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