1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
5 <title>stripe tables</title>
6 <style type="text/css">
7 * {
8 padding:0;
9 margin:0;
10 }
11
12 body {
13 font-family:verdana;
14 font-size:12px;
15 }
16
17 .content {
18 width:550px;
19 margin:20px auto;
20 }
21
22 .content h1 {
23 font-family:'微软雅黑';
24 font-size:18px;
25 padding-bottom:5px;
26 }
27
28 table {
29 width:100%;
30 }
31
32 th, td {
33 padding:6px 0;
34 text-align:center;
35 }
36
37 th {
38 background-color:#007D28;
39 color:#ffffff;
40 }
41
42 tr {
43 background-color:#E8FFE8;
44 }
45
46 .odd {
47 background-color:#FFF3EE;
48 }
49
50 .highlight {
51 background-color:#E0E0E0;
52 }
53
54 </style>
55
56 <script type="text/javascript">
57 //动态给js添加class属性
58 function addClass(element, value) {
59 if(!element.className) {
60 element.className = value; //如果element本身不存在class,则直接添加class为value的值
61 } else {
62 element.className += " "+value; //如果element之前有一个class值,注意中间要多一个空格,然后再加上value的值
63 }
64 }
65
66 function stripeTable() {
67 var tables = document.getElementsByTagName("table"); //遍历文档中的所有table
68 for(var i=0; i<tables.length; i++) {
69 var rows = document.getElementsByTagName("tr");
70 for(var j=0; j<rows.length; j++) {
71 if(j%2 == 0) {
72 addClass(rows[j], "odd"); //如是偶数行,则添加class为odd的属性
73 //rows[j].setAttribute("class", "odd");
74 }
75 }
76 }
77 }
78
79 function highlightRows() {
80 var rows = document.getElementsByTagName("tr");
81 for(var i=0; i<rows.length; i++) {
82 rows[i].oldClassName = rows[i].className; //首先保存之前的class值
83 rows[i].onmouseover = function() {
84 addClass(this, "highlight"); //鼠标经过时添加class为highlight的值
85 }
86 rows[i].onmouseout = function() {
87 this.className = this.oldClassName; //鼠标离开时还原之前的class值
88 }
89 }
90 }
91
92 window.onload = function() {
93 stripeTable();
94 highlightRows();
95 }
96 </script>
97
98
99
100 </head>
101
102 <body>
103 <div class="content">
104 <h1>会员信息表</h1>
105 <table summary="user infomation table">
106 <thead>
107 <tr>
108 <th>会员ID</th>
109 <th>会员名</th>
110 <th>邮箱</th>
111 <th>会员组</th>
112 <th>城市</th>
113 <th>注册时间</th>
114 </tr>
115 </thead>
116 <tbody>
117 <tr>
118 <td>126</td>
119 <td>webw3c</td>
120 <td>este@126.com</td>
121 <td>普通会员</td>
122 <td>武汉</td>
123 <td>2011-04-01</td>
124 </tr>
125 <tr>
126 <td>127</td>
127 <td>test001</td>
128 <td>test001@126.com</td>
129 <td>普通会员</td>
130 <td>北京</td>
131 <td>2011-04-03</td>
132 </tr>
133 <tr>
134 <td>128</td>
135 <td>wuliao</td>
136 <td>wuliao@126.com</td>
137 <td>普通会员</td>
138 <td>南昌</td>
139 <td>2011-04-04</td>
140 </tr>
141 <tr>
142 <td>129</td>
143 <td>tired</td>
144 <td>tired@126.com</td>
145 <td>中级会员</td>
146 <td>北京</td>
147 <td>2011-04-06</td>
148 </tr>
149 <tr>
150 <td>130</td>
151 <td>tiredso</td>
152 <td>tireds0@126.com</td>
153 <td>中级会员</td>
154 <td>武汉</td>
155 <td>2011-04-06</td>
156 </tr>
157 <tr>
158 <td>131</td>
159 <td>javascript</td>
160 <td>js2011@126.com</td>
161 <td>中级会员</td>
162 <td>武汉</td>
163 <td>2011-04-08</td>
164 </tr>
165 <tr>
166 <td>132</td>
167 <td>jQuery</td>
168 <td>jQuery@126.com</td>
169 <td>高级会员</td>
170 <td>北京</td>
171 <td>2011-04-12</td>
172 </tr>
173 </tbody>
174 </table>
175 </div>
176 </body>
177
178 </html>