我使用javascript往表格里面添加1000行,居然花费了 3 秒。我想请问下有什么方法能让 javascript 往表格里面添加行能快点。我的代码如下:<html>
<body>
测试动态添加表格的效率
<input type="button" value="测试方法一" onclick="g()"/>
<input type="button" value="测试方法二" onclick="g2()"/>
</body>
<script language="javascript">
var rNum = 1000;
function g()
{
var d = new Date();
var tbl = document.createElement("TABLE"); for(var i = 0;i < rNum;i++)
{
var tr = tbl.insertRow();
tr.Height = 30;
var cel = tr.insertCell();
cel.innerHTML = "sdfsdf";
}
document.body.appendChild(tbl);
var n = new Date();
alert("花费时间:" + (n-d)/1000)
}
function g2()
{
var d = new Date();
var tbl = document.createElement("TABLE");
var tr = tbl.insertRow();
tr.Height = 30;
var cel = tr.insertCell();
cel.innerHTML = "sdfsdf";
for(var i = 0;i < rNum;i++)
{
var n = tbl.rows[0].cloneNode(true);
tbl.childNodes[0].appendChild(n);
}
document.body.appendChild(tbl);
var n = new Date();
alert("花费时间:" + (n-d)/1000)
}
</script></html>
<body>
测试动态添加表格的效率
<input type="button" value="测试方法一" onclick="g()"/>
<input type="button" value="测试方法二" onclick="g2()"/>
</body>
<script language="javascript">
var rNum = 1000;
function g()
{
var d = new Date();
var tbl = document.createElement("TABLE"); for(var i = 0;i < rNum;i++)
{
var tr = tbl.insertRow();
tr.Height = 30;
var cel = tr.insertCell();
cel.innerHTML = "sdfsdf";
}
document.body.appendChild(tbl);
var n = new Date();
alert("花费时间:" + (n-d)/1000)
}
function g2()
{
var d = new Date();
var tbl = document.createElement("TABLE");
var tr = tbl.insertRow();
tr.Height = 30;
var cel = tr.insertCell();
cel.innerHTML = "sdfsdf";
for(var i = 0;i < rNum;i++)
{
var n = tbl.rows[0].cloneNode(true);
tbl.childNodes[0].appendChild(n);
}
document.body.appendChild(tbl);
var n = new Date();
alert("花费时间:" + (n-d)/1000)
}
</script></html>
同时还开着2个游戏