对于创建表格本文提到的算法共有
种
分别是直接操作dom
借助createDocumentFragment
借助js的原生表格操作方法
我们先看一下三种算法以及在各种浏览器下的表现
第一种 直接操作dom
复制代码 代码如下:
<!DOCTYPE HTML PUBLIC "
//W
C//DTD HTML
Transitional//EN"
"
<html xmlns="
<head>
<meta http
equiv="Content
Type" content="text/html; charset=utf
" />
<title>New Web Project</title>
</head>
<body>
<script>
microtime = function(get_as_float) {
var now = new Date()
getTime() /
;
var s = parseInt(now
);
return (get_as_float) ? now : (Math
round((now
s) *
) /
) +
+ s;
}
var m
= microtime(true);
var table = document
createElement("table");
table
border =
;
var tbody = document
createElement("tbody");
for(var i =
; i <
; i++ ) {
var tr = document
createElement("tr");
for(var j =
; j <
; j++ ) {
var td = document
createElement("td");
td
appendChild(document
createTextNode("cell "+i+"
"+j));
tr
appendChild(td);
}
tbody
appendChild(tr);
}
table
appendChild(tbody);
//chrome
//ie
//ie
//ie
//ie
//firefox
//opera
//safari
document
body
appendChild(table);
var m
= microtime(true);
alert(m
m
);
</script>
</body>
</html>
第二种借助借助createDocumentFragment
复制代码 代码如下:
<!DOCTYPE HTML PUBLIC "
//W
C//DTD HTML
Transitional//EN"
"
<html xmlns="
<head>
<meta http
equiv="Content
Type" content="text/html; charset=utf
" />
<title>New Web Project</title>
</head>
<body>
<script>
microtime = function(get_as_float) {
var now = new Date()
getTime() /
;
var s = parseInt(now
);
return (get_as_float) ? now : (Math
round((now
s) *
) /
) +
+ s;
}
var m
= microtime(true);
var table = document
createElement("table");
table
border =
;
var tbody = document
createElement("tbody");
var fragment = document
createDocumentFragment();
for(var i =
; i <
; i++ ) {
var tr = document
createElement("tr");
for(var j =
; j <
; j++ ) {
var td = document
createElement("td");
td
appendChild(document
createTextNode("cell "+i+"
"+j));
tr
appendChild(td);
}
fragment
appendChild(tr);
}
tbody
appendChild(fragment);
table
appendChild(tbody);
//chrome
//ie
//ie
//ie
//ie
//firefox
//opera
//safari
document
body
appendChild(table);
var m
= microtime(true);
alert(m
m
);
</script>
</body>
</html>
第三种借助js的原生表格操作方法
复制代码 代码如下:
<!DOCTYPE HTML PUBLIC "
//W
C//DTD HTML
Transitional//EN"
"
<html xmlns="
<head>
<meta http
equiv="Content
Type" content="text/html; charset=utf
" />
<title>New Web Project</title>
</head>
<body>
<script>
microtime = function(get_as_float) {
var now = new Date()
getTime() /
;
var s = parseInt(now
);
return (get_as_float) ? now : (Math
round((now
s) *
) /
) +
+ s;
}
var m
= microtime(true);
var table = document
createElement("table");
table
border =
;
var tbody = document
createElement("tbody");
table
appendChild(tbody);
for(var i =
; i <
; i++ ) {
tbody
insertRow(i);
for(var j =
; j <
; j++ ) {
tbody
rows[i]
insertCell(j);
tbody
rows[i]
cells[j]
appendChild(document
createTextNode("cell "+i+"
"+j));
}
var tr = document
createElement("tr");
tbody
appendChild(tr);
}
//chrome
//ie
//ie
//ie
//ie
//firefox
//opera
//safari
document
body
appendChild(table);
var m
= microtime(true);
alert(m
m
);
</script>
</body>
</html>
以上可以看出用原生的js创建表格效率最好借助createDocumentFragment优势不是很大(并不像网上说的那么明显)借助insertRow和insertCell等在ie下效率太低不建议使用
对第一种算法简单的优化了一下
复制代码 代码如下:
<!DOCTYPE HTML PUBLIC "
//W
C//DTD HTML
Transitional//EN"
"
<html xmlns="
<head>
<meta http
equiv="Content
Type" content="text/html; charset=utf
" />
<title>New Web Project</title>
</head>
<body>
<script>
microtime = function(get_as_float) {
var now = new Date()
getTime() /
;
var s = parseInt(now
);
return (get_as_float) ? now : (Math
round((now
s) *
) /
) +
+ s;
}
var m
= microtime(true);
var table = document
createElement("table");
table
border =
;
var tbody = document
createElement("tbody");
var i =
;
while(i
){
var tr = document
createElement("tr")
j =
;
while(j
){
var td = document
createElement("td");
td
appendChild(document
createTextNode("cell "+i+"
"+j));
tr
appendChild(td);
}
tbody
appendChild(tr);
}
table
appendChild(tbody);
//chrome
//ie
//ie
//ie
//ie
//firefox
//opera
//safari
document
body
appendChild(table);
var m
= microtime(true);
alert(m
m
);
</script>
</body>
</html>
总结对dom操作尽量使用createElement和appendChild对于js内核提供的一些特殊方法慎用