document.createElement("table")只是创建了一个对象,如果要把它添加到document中,还需要另外的方法(appendChild、insertAdjacentElement、insertBefore)
例子:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
</head>
<body>
<table id="tab1" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td> </td></tr>
</table>
<script language="JavaScript">
alert(document.documentElement.innerHTML) //查看添加对象以前的源码
oNewTable = document.createElement("table")
//以下三种方法任选一种测试,注释掉其余两种
document.body.appendChild(oNewTable) //增加成为body的最后一个子元素
document.body.insertBefore(oNewTable, tab1) //增加成为body的子元素,位于tab1的前面
tab1.insertAdjacentElement("beforeBegin", oNewTable) //插入到tab1的前面(tab1是上面那表格对象的ID)
//beforeBegin还可以换成"afterBegin", "beforeEnd" , "afterEnd"
alert(document.documentElement.innerHTML) //查看添加对象以后的源码
//更具体的用法请参考MSDN:http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/dhtml_reference_entry.asp
</script>
</body>
</html>
例子:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
</head>
<body>
<table id="tab1" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td> </td></tr>
</table>
<script language="JavaScript">
alert(document.documentElement.innerHTML) //查看添加对象以前的源码
oNewTable = document.createElement("table")
//以下三种方法任选一种测试,注释掉其余两种
document.body.appendChild(oNewTable) //增加成为body的最后一个子元素
document.body.insertBefore(oNewTable, tab1) //增加成为body的子元素,位于tab1的前面
tab1.insertAdjacentElement("beforeBegin", oNewTable) //插入到tab1的前面(tab1是上面那表格对象的ID)
//beforeBegin还可以换成"afterBegin", "beforeEnd" , "afterEnd"
alert(document.documentElement.innerHTML) //查看添加对象以后的源码
//更具体的用法请参考MSDN:http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/dhtml_reference_entry.asp
</script>
</body>
</html>