<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>add.html</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
/**为表格添加一行**/
function test1(){//方式一
var t = document.getElementById("tab");
var tr = document.createElement("tr");
var td = document.createElement("td");
td.innerHTML = 'bbb';
tr.appendChild(td);
t.appendChild(tr);
}
function test2(){//方式二
var t = document.getElementById("tab");
var row = t.insertRow(t.rows.length);
var td = row.insertCell(0);
td.innerHTML = 'bbb';
}
</script>
</head>
<body>
<button onclick="test1();">添加</button> <button onclick="test2();">添加2</button>
<table id="tab" border="1">
<tr><td>aaaa</td></tr>
</table>
</body>
</html>方式一、方式二都能为表格增加一行,请问它们有什么区别和联系?方式一叫什么,方式二叫什么?本人初学,不要笑话我。htmltable
<html>
<head>
<title>add.html</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
/**为表格添加一行**/
function test1(){//方式一
var t = document.getElementById("tab");
var tr = document.createElement("tr");
var td = document.createElement("td");
td.innerHTML = 'bbb';
tr.appendChild(td);
t.appendChild(tr);
}
function test2(){//方式二
var t = document.getElementById("tab");
var row = t.insertRow(t.rows.length);
var td = row.insertCell(0);
td.innerHTML = 'bbb';
}
</script>
</head>
<body>
<button onclick="test1();">添加</button> <button onclick="test2();">添加2</button>
<table id="tab" border="1">
<tr><td>aaaa</td></tr>
</table>
</body>
</html>方式一、方式二都能为表格增加一行,请问它们有什么区别和联系?方式一叫什么,方式二叫什么?本人初学,不要笑话我。htmltable
相同之处都为对象的方法.
appendChild为对象添加子对象.
insertRow是为对象插入航,是table对象特有方法.
insertCell是为对象插入列,是tr对象特有方法.
方法一,是先建立两个对象,分别为行对象跟列对象,然后把行对象添加到表格对象中,把列对象添加到行对象中,分别作为其子对象.
方法二.直接为表格调用方法,插入行,然后为插入的行调用方法插入列来达到添加表格的目的.
appendChild方法,很多对象有这个方法,通用性更强一些.