-
Notifications
You must be signed in to change notification settings - Fork 0
/
浏览器渲染原理.html
65 lines (61 loc) · 2.12 KB
/
浏览器渲染原理.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>浏览器渲染原理</title>
</head>
<body>
<button id="normal">正常</button>
<button id="display">隐藏后展示</button>
<button id="fragment">fragment</button>
<button id="backup">备份</button>
<button id="clear">清空</button>
<ul id="list"></ul>
<script>
const data = [];
for (let i = 0; i < 2000; i++) {
data.push(i);
}
function appendDataToElement(appendToElement, data) {
let li;
for (let i = 0; i < data.length; i++) {
li = document.createElement('li');
li.textContent = 'text';
appendToElement.appendChild(li);
}
}
const ul = document.getElementById('list');
const normalBtn = document.getElementById('normal');
const displayBtn = document.getElementById('display');
const fragment = document.getElementById('fragment');
const backup = document.getElementById('backup');
const clear = document.getElementById('clear');
normalBtn.addEventListener('click', () => {
appendDataToElement(ul, data);
});
displayBtn.addEventListener('click', () => {
ul.style.display = 'none';
appendDataToElement(ul, data);
ul.style.display = 'block';
});
fragment.addEventListener('click', () => {
const frag = document.createDocumentFragment();
appendDataToElement(frag, data);
ul.appendChild(frag);
})
backup.addEventListener('click', () => {
const clone = ul.cloneNode(true);
appendDataToElement(clone, data);
ul.parentNode.replaceChild(clone, ul);
ul = clone;
})
clear.addEventListener('click', () => {
while (ul.lastChild) {
ul.removeChild(ul.lastChild);
}
})
</script>
</body>
</html>