在实际项目开发中,我们需要的表格的行,列,背景色,都可能是动态的,因此利用JS做成一个函数,在我们需要的地方直接调用这个函数,是一个非常好的方法,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<html xmlns=
"http://www.w3.org/1999/xhtml"
>
<head>
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=utf-8"
/>
<title>动态制作表格</title>
<script type=
"text/javascript"
>
table(8,2,
"red"
);
function
table(row,col,color){
document.write(
'<table border="1" width="800" align="center">'
);
for
(
var
i=0;i<row;i++){
if
(i%2==0)
var
bg=color;
else
var
bg=
""
;
document.write(
'<tr bgcolor="'
+bg+
'">'
);
for
(
var
j=1;j<=col;j++)
document.write(
'<td>'
+(i*row+col)+
'</td>'
);
document.write(
'</tr>'
);
}
document.write(
'</table>'
);
}
</script>
</head>
<body>
</body>
</html>
|
解释:
table(8,2,"red");传进来就是实际参数,可以动态传进来
table(row,col,color)接受的就是形参
这样,不论是行,列,背景色,宽度,高度等都已作为一个形参,传进来!
OK!
本文转自 小夜的传说 51CTO博客,原文链接:http://blog.51cto.com/1936625305/1432146,如需转载请自行联系原作者