三层可折叠table

这篇博客介绍了如何使用JavaScript实现三层可折叠的表格功能。通过点击图片触发事件,改变图片旋转角度并切换相应表格行的显示状态,实现了表格的展开和收起效果。涉及CSS3的transform属性以及DOM操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

table.js


table.css

@CHARSET "UTF-8";

* {
	margin: 0;
	padding: 0;
}

body {
	padding: 10px 10px;
}

.demo {
	width: 600px;
	margin: 40px auto;
	font-family: 'trebuchet MS', 'Lucida sans', Arial;
	font-size: 14px;
	color: #444;
}

table {
	*border-collapse: collapse; /* IE7 and lower */
	border-spacing: 0;
	width: 100%;
	margin-top: 10px;
}
/*========bordered table========*/
.bordered {
	border: solid #ccc 1px;
	-moz-border-radius: 10px;
	-webkit-border-radius: 10px;
	border-radius: 10px;
	-webkit-box-shadow: 0 1px 1px #ccc;
	-moz-box-shadow: 0 1px 1px #ccc;
	box-shadow: 0 1px 1px #ccc;
}

.bordered tr {
	-o-transition: all 0.1s ease-in-out;
	-webkit-transition: all 0.1s ease-in-out;
	-moz-transition: all 0.1s ease-in-out;
	-ms-transition: all 0.1s ease-in-out;
	transition: all 0.1s ease-in-out;
}

.bordered .highlight, /* .bordered tr:hover {
				background: #fbf8e9;		
			} */ .bordered td,.bordered th {
	border-left: 1px solid #ccc;
	border-top: 1px solid #ccc;
	padding: 10px;
	text-align: left;
}

.bordered th {
	background-color: #FBFBFC;
	background-image: -webkit-gradient(linear, left top, left bottom, from(#FBFBFC),
		to(#FBFBFC) );
	background-image: -webkit-linear-gradient(top, #FBFBFC, #FBFBFC);
	background-image: -moz-linear-gradient(top, #FBFBFC, #FBFBFC);
	background-image: -ms-linear-gradient(top, #FBFBFC, #FBFBFC);
	background-image: -o-linear-gradient(top, #FBFBFC, #FBFBFC);
	background-image: linear-gradient(top, #FBFBFC, #FBFBFC);
	filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0,
		startColorstr=#ebf3fc, endColorstr=#dce9f9 );
	-ms-filter:
		"progid:DXImageTransform.Microsoft.gradient (GradientType=0, startColorstr=#ebf3fc, endColorstr=#dce9f9)";
	-webkit-box-shadow: 0 1px 0 rgba(255, 255, 255, .8) inset;
	-moz-box-shadow: 0 1px 0 rgba(255, 255, 255, .8) inset;
	box-shadow: 0 1px 0 rgba(255, 255, 255, .8) inset;
	border-top: none;
	text-shadow: 0 1px 0 rgba(255, 255, 255, .5);
}

.bordered td:first-child,.bordered th:first-child {
	border-left: none;
}

.bordered th:first-child {
	-moz-border-radius: 10px 0 0 10px;
	-webkit-border-radius: 10px 0 0 10px;
	border-radius: 10px 0 0 10px;
}

.bordered th:last-child {
	-moz-border-radius: 0 10px 10px 0;
	-webkit-border-radius: 0 10px 10px 0;
	border-radius: 0 10px 10px 0;
}

.bordered tr:last-child td:first-child {
	-moz-border-radius: 0 0 0 10px;
	-webkit-border-radius: 0 0 0 10px;
	border-radius: 0 0 0 10px;
}

.bordered tr:last-child td:last-child {
	-moz-border-radius: 0 0 10px 0;
	-webkit-border-radius: 0 0 10px 0;
	border-radius: 0 0 10px 0;
}
/*----------------------*/
.zebra td,.zebra th {
	padding: 10px;
	border-bottom: 1px solid #f2f2f2;
}

.zebra .alternate,.zebra tbody tr:nth-child(even) {
	background: #f5f5f5;
	-webkit-box-shadow: 0 1px 0 rgba(255, 255, 255, .8) inset;
	-moz-box-shadow: 0 1px 0 rgba(255, 255, 255, .8) inset;
	box-shadow: 0 1px 0 rgba(255, 255, 255, .8) inset;
}

.zebra th {
	text-align: left;
	text-shadow: 0 1px 0 rgba(255, 255, 255, .5);
	border-bottom: 1px solid #ccc;
	background-color: #eee;
	background-image: -webkit-gradient(linear, left top, left bottom, from(#f5f5f5),
		to(#eee) );
	background-image: -webkit-linear-gradient(top, #f5f5f5, #eee);
	background-image: -moz-linear-gradient(top, #f5f5f5, #eee);
	background-image: -ms-linear-gradient(top, #f5f5f5, #eee);
	background-image: -o-linear-gradient(top, #f5f5f5, #eee);
	background-image: linear-gradient(top, #f5f5f5, #eee);
	filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0,
		startColorstr=#f5f5f5, endColorstr=#eeeeee );
	-ms-filter:
		"progid:DXImageTransform.Microsoft.gradient (GradientType=0, startColorstr=#f5f5f5, endColorstr=#eeeeee)";
}

.zebra th:first-child {
	-moz-border-radius: 10px 0 0 10px;
	-webkit-border-radius: 10px 0 0 10px;
	border-radius: 10px 0 0 10px;
}

.zebra th:last-child {
	-moz-border-radius: 0 10px 10px 0;
	-webkit-border-radius: 0 10px 10px 0;
	border-radius: 0 10px 10px 0;
}

.zebra tfoot td {
	border-bottom: 0;
	border-top: 1px solid #fff;
	background-color: #f1f1f1;
}

.zebra tfoot td:first-child {
	-moz-border-radius: 0 0 0 10px;
	-webkit-border-radius: 0 0 0 10px;
	border-radius: 0 0 0 10px;
}

.zebra tfoot td:last-child {
	-moz-border-radius: 0 0 10px 0;
	-webkit-border-radius: 0 0 10px 0;
	border-radius: 0 0 10px 0;
}

/////////////////////////////////////////////////////
.img_css {
	vertical-align: middle;
	margin-right: 10px;
	margin-bottom: 3px;
}

table.html

<!DOCTYPE html>
<html>
  <head>
    <title>table.html</title>
    <meta charset="utf-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1">  
	<link rel="stylesheet" href="css/table.css" type="text/css"></link>
	<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
  	<script type="text/javascript" src="js/table.js"></script>
  </head>
  
  <body>
    <table class="bordered">
		<thead>
			<tr id="title1">
				<th colspan="2">[3号线]</th>        
			    <th style="text-align: right; border-left: none; width: 15%;">
			    	 <img id="img_title1" width="27" src="images/u122.png" class="img_title_css" />
			    	 <!-- <span id="span_title1"  width="5" style="visibility: hidden;">0</span> -->
			    	 <span id="span_title1"  width="5" style="display: none;">0</span>
			    </th>
			</tr>
		</thead>
		<tbody id="tbody_title1" style="display:null;">
			<tr class="parent" id="row123">
				<td colspan="2" style="width: 85%;">高新区间</td>
				<td style="text-align: right; border-left: none; width: 15%;">
					<img id="img_row123" width="27" src="images/u122.png" class="img_css" />
					<span id="span_row123" style="display: none;">0</span>
				</td>
			</tr>
			<tr class="child-row123">
				<td style="width: 30%;">1</td>        
			    <td style="width: 40%;">The Shawshank Redemption</td>
			    <td style="width: 30%;">1994</td>
			</tr>        
			<tr class="child-row123">
				<td>2</td>         
			    <td>The Godfather</td>
			    <td>1972</td>
			</tr>
			<tr class="parent" id="row456">
				<td colspan="2" style="width: 66.7%;">珉良区间</td>
				<td style="text-align: right; border-left: none; width: 15%;">
					<img id="img_row456" width="27" src="images/u122.png" class="img_css" />
					<span id="span_row456" style="display: none;">0</span>
				</td>
			</tr>
			<tr class="child-row456">
				<td>1</td>        
			    <td>The Shawshank Redemption</td>
			    <td>1994</td>
			</tr>        
			<tr class="child-row456">
				<td>2</td>         
			    <td>The Godfather</td>
			    <td>1972</td>
			</tr>
		</tbody>
	</table>
	<table class="bordered">
		<thead>
			<tr id="title2">
				<th colspan="2">[3号线]</th>        
			    <th style="text-align: right; border-left: none; width: 15%;">
			    	 <img id="img_title2" width="27" src="images/u122.png" class="img_title_css" />
			    	 <!-- <span id="span_title1"  width="5" style="visibility: hidden;">0</span> -->
			    	 <span id="span_title2"  width="5" style="display: none;">0</span>
			    </th>
			</tr>
		</thead>
		<tbody id="tbody_title2" style="display:null;">
			<tr class="parent" id="row1231">
				<td colspan="2" style="width: 85%;">高新区间</td>
				<td style="text-align: right; border-left: none; width: 15%;">
					<img id="img_row1231" width="27" src="images/u122.png" class="img_css" />
					<span id="span_row1231" style="display: none;">0</span>
				</td>
			</tr>
			<tr class="child-row1231">
				<td style="width: 30%;">1</td>        
			    <td style="width: 40%;">The Shawshank Redemption</td>
			    <td style="width: 30%;">1994</td>
			</tr>        
			<tr class="child-row1231">
				<td>2</td>         
			    <td>The Godfather</td>
			    <td>1972</td>
			</tr>
			<tr class="parent" id="row4561">
				<td colspan="2" style="width: 85%;">珉良区间</td>
				<td style="text-align: right; border-left: none; width: 15%;">
					<img id="img_row4561" width="27" src="images/u122.png" class="img_css" />
					<span id="span_row4561" style="display: none;">0</span>
				</td>
			</tr>
			<tr class="child-row4561">
				<td>1</td>        
			    <td>The Shawshank Redemption</td>
			    <td>1994</td>
			</tr>        
			<tr class="child-row4561">
				<td>2</td>         
			    <td>The Godfather</td>
			    <td>1972</td>
			</tr>
		</tbody>
	</table>
  </body>
</html>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值