document.write (javastr) 将重写网页全部内容
应在插入内容的位置事先,写一个HTML标记,如<span ID=insTAB></span>
然后将document.write (javastr)改为:
insTAB.innerHTML=javastr;说明:span也可以是其它标记:如DIV、TD、P等。
应在插入内容的位置事先,写一个HTML标记,如<span ID=insTAB></span>
然后将document.write (javastr)改为:
insTAB.innerHTML=javastr;说明:span也可以是其它标记:如DIV、TD、P等。
document.body.innerHTML=javastr;
2、把脚本放到body之前去应该就可以了
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="javascript">
function insert_table()
{
var javastr='';
javastr=javastr+"<table width=\"100%\" border=\"0\">"javastr=javastr+"<tr><td>"
javastr=javastr+"你好,你好,你好,你好 "
javastr=javastr+"</td></tr>"javastr=javastr+"<tr><td>"
javastr=javastr+"你好,你好,你好,你好 "
javastr=javastr+"</td></tr>"
javastr=javastr+"</table>" id1.innerHTML+=javastr;
}
</script></head><body bgcolor="#FFFFFF" text="#000000">
<div id=id1></div>
<input type="button" onclick="insert_table()" value="新表">
</body>
</html>
<TABLE>
<TBODY>
<TR>
<TD>你好!你好!你好!你好!</TD>
</TR>
<TR>
<TD>你好!你好!你好!你好!</TD>
</TR>
</TBODY>
</TABLE>但是你用write这种办法很笨拙的,我教你一个新方法。(以下是VBScript语法,我不懂JavaScript)Set tTable=createElement("TABLE") '建立TBALE元素
Set tTBody=createElement("TBODY") '建立TBODY元素tTable.insertBefore tTBody '把TBODY元素插到TABLE里。With tTable
.width="100%"
.border=0
End WithSet tRow=tTBody.insertRow '插入行
Set tCell=tRow.insertCell '插入单元格
tCell.innerText="你好!你好!你好!你好!"Set tRow=tTBody.insertRow
Set tCell=tRow.insertCell
tCell.innerText="你好!你好!你好!你好!"document.body.insertBefore tTable注意:tTable和tCell等均是对元素的“引用”。我最近编写的一个菜单程序和扫雷游戏都是用这个方法,它的优点在于是对元素操作的,非常灵活。而write方法你仅仅是把元素看成代码而已。以上程序看起来似乎很烦琐,其实是因为没有写成函数的缘故。如果你将我上面的程序以函数来实现,那就不会出现:
Set tRow=tTBody.insertRow
Set tCell=tRow.insertCell
tCell.innerText="你好!你好!你好!你好!"
这段的重复出现了。