http://www.softwhy.com/forum.php?mod=viewthread&tid=15098
js迭代table表格的行和列代码实例:
表格是最为常用的元素之一,虽然已经没有以前那么火热,但并不表示它就没有市场了,现在对于它的应用的定位也越来越恰当和准确,也就是说它现在基本已经不会被用在布局上,而是用在对数据的组织上,回归正题,下面就介绍一下如何迭代table表格的行列。
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
<
html
>
<
head
>
<
meta
charset
=
" utf-8"
>
<
meta
name
=
"author"
content
=
"http://www.softwhy.com/"
/>
<
title
>蚂蚁部落
title
>
<
style
type
=
"text/css"
>
.table{
width:300px;
height:100px;
border:1px solid #ccc;
border-collapse:collapse;
}
.table td,.table th {
border:1px solid #ccc;
padding:5px;
}
style
>
<
script
type
=
"text/javascript"
>
window.onload=function(){
var otable=document.getElementById("antzone");
var odiv=document.getElementById("show");
var num=0;
for(var index=0;index<
otable.rows.length
;index++){
num
=num+1;
}
odiv.innerHTML
=
num
;
}
head
>
<
body
>
<
div
id
=
"show"
>
div
>
<
table
id
=
"antzone"
class
=
"table"
>
<
thead
>
<
tr
>
<
th
>蚂蚁部落一
th
>
<
th
>蚂蚁部落二
th
>
tr
>
thead
>
<
tr
>
<
td
>javascript教程
td
>
<
td
>jQuery教程
td
>
tr
>
<
tr
>
<
td
>HTML教程
td
>
<
td
>div css教程
td
>
tr
>
table
>
body
>
html
>
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
<
html
>
<
head
>
<
meta
charset
=
" utf-8"
>
<
meta
name
=
"author"
content
=
"http://www.softwhy.com/"
/>
<
title
>蚂蚁部落
title
>
<
style
type
=
"text/css"
>
.table{
width:300px;
height:100px;
border:1px solid #ccc;
border-collapse:collapse;
}
.table td,.table th {
border:1px solid #ccc;
padding:5px;
}
style
>
<
script
type
=
"text/javascript"
>
window.onload=function(){
var otable=document.getElementById("antzone");
var odiv=document.getElementById("show");
var num=0;
for(var index=0;index<
otable.rows
[0].cells.length;index++){
num
=num+1;
}
odiv.innerHTML
=
num
;
}
head
>
<
body
>
<
div
id
=
"show"
>
div
>
<
table
id
=
"antzone"
class
=
"table"
>
<
thead
>
<
tr
>
<
th
>蚂蚁部落一
th
>
<
th
>蚂蚁部落二
th
>
tr
>
thead
>
<
tr
>
<
td
>javascript教程
td
>
<
td
>jQuery教程
td
>
tr
>
<
tr
>
<
td
>HTML教程
td
>
<
td
>div css教程
td
>
tr
>
table
>
body
>
html
>
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
<
html
>
<
head
>
<
meta
charset
=
" utf-8"
>
<
meta
name
=
"author"
content
=
"http://www.softwhy.com/"
/>
<
title
>蚂蚁部落
title
>
<
style
type
=
"text/css"
>
.table{
width:300px;
height:100px;
border:1px solid #ccc;
border-collapse:collapse;
}
.table td,.table th {
border:1px solid #ccc;
padding:5px;
}
style
>
<
script
type
=
"text/javascript"
>
window.onload=function(){
var otable=document.getElementById("antzone");
var odiv=document.getElementById("show");
var arr=[];
for(var index=0;index<
otable.rows.length
;index++){
for(var
j
=
0
;j
arr.push(otable.rows[index].cells[j].innerHTML);
}
}
odiv.innerHTML
=
arr
.toString();
}
head
>
<
body
>
<
div
id
=
"show"
>
div
>
<
table
id
=
"antzone"
class
=
"table"
>
<
thead
>
<
tr
>
<
th
>蚂蚁部落一
th
>
<
th
>蚂蚁部落二
th
>
tr
>
thead
>
<
tr
>
<
td
>javascript教程
td
>
<
td
>jQuery教程
td
>
tr
>
<
tr
>
<
td
>HTML教程
td
>
<
td
>div css教程
td
>
tr
>
table
>
body
>
html
>
|