- ·上一篇文章:使用JavaScript和DOM创建HTML表格
- ·下一篇文章:js实现日期下拉框
当前位置:K88软件开发 → 文章中心 → 编程语言 → JavaScript → JS01 → 文章内容
javascript动态改变表格单元格中的内容
<
TABLE 
id="
table1"
 
border=1 
width="
150"
>
<
TR>
<
TD 
align="
center"
 
width="
50%"
>
0,0<
/TD>
<
TD 
align="
center"
>
0,1<
/TD>
<
/TR>
<
TR>
<
TD 
align="
center"
>
1,0<
/TD>
<
TD 
align="
center"
>
1,1<
/TD>
<
/TR>
<
/TABLE>
<
form 
name="
form1"
 
method="
post"
 
action="
"
>
输入要添加的文字:<
br>
<
input 
type="
text"
 
name="
f1"
>
<
br>
行<
input 
type="
text"
 
size="
5"
 
name="
row"
 
value="
0"
>
&
#38
nbsp
列<
input 
type="
text"
 
size="
5"
 
name="
col"
 
value="
0"
>
<
br>
<
input 
type="
button"
 
value="
添加"
 
onclick="
f2()"
>
<
/form>
<
script>
 
 
function 
f2()
{
col=parseInt(document.form1.col.value)
 
 
row=parseInt(document.form1.row.value)
f1=document.form1.f1.value
document.getElementById("
table1"
).rows.item(row).cells.item(col).innerHTML=f1
}
<
/script>
TABLE 
id="
table1"
 
border=1 
width="
150"
>
<
TR>
<
TD 
align="
center"
 
width="
50%"
>
0,0<
/TD>
<
TD 
align="
center"
>
0,1<
/TD>
<
/TR>
<
TR>
<
TD 
align="
center"
>
1,0<
/TD>
<
TD 
align="
center"
>
1,1<
/TD>
<
/TR>
<
/TABLE>
<
form 
name="
form1"
 
method="
post"
 
action="
"
>
输入要添加的文字:<
br>
<
input 
type="
text"
 
name="
f1"
>
<
br>
行<
input 
type="
text"
 
size="
5"
 
name="
row"
 
value="
0"
>
&
#38
nbsp
列<
input 
type="
text"
 
size="
5"
 
name="
col"
 
value="
0"
>
<
br>
<
input 
type="
button"
 
value="
添加"
 
onclick="
f2()"
>
<
/form>
<
script>
 
 
function 
f2()
{
col=parseInt(document.form1.col.value)
 
 
row=parseInt(document.form1.row.value)
f1=document.form1.f1.value
document.getElementById("
table1"
).rows.item(row).cells.item(col).innerHTML=f1
}
<
/script>
javascript动态改变表格单元格中的内容