-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjQuery之documentFragment.html
46 lines (38 loc) · 1.16 KB
/
jQuery之documentFragment.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery之domMainp和documentFragment</title>
</head>
<body>
<script src="jQuery.js"></script>
<button id="Button1" onclick = "a1()">普通方式创建</button>
<button id="Button2" onclick = "a2()">documentFragment创建</button>
<div id="test1"></div>
<div id="test2"></div>
<script type="text/javascript">
function a1() {
console.time("普通方式创建")
for (let i = 0; i < 5000; i++) {
let op = document.createElement("span");
let oText = document.createTextNode(i);
op.appendChild(oText);
document.body.appendChild(op);
}
console.timeEnd("普通方式创建")
}
function a2() {
console.time("documentFragment创建")
let oFragmeng = document.createDocumentFragment(); //创建文档碎片
for (let i = 0; i < 5000; i++) {
let op = document.createElement("span");
let oText = document.createTextNode(i);
op.appendChild(oText);
oFragmeng.appendChild(op);
}
document.body.appendChild(oFragmeng); //最后一次性添加到document中
console.timeEnd("documentFragment创建")
}
</script>
</body>
</html>