-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_create_document_fragment.js
69 lines (59 loc) · 2.24 KB
/
example_create_document_fragment.js
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
66
67
68
// example_create_document_fragment.js
// Free to use under the GPLv3 and later
/*
This script demonstrates how to use the createDocumentFragment method to
efficiently create and add multiple nodes to the DOM. It covers the basics
of using DocumentFragment for batch DOM manipulation.
*/
// Wait for the DOM content to load before executing the script
document.addEventListener('DOMContentLoaded', function() {
// Selecting the container element
const container = document.querySelector('#container');
// Creating a DocumentFragment
const fragment = document.createDocumentFragment();
// Creating multiple elements and appending them to the fragment
for (let i = 1; i <= 5; i++) {
const newItem = document.createElement('div');
newItem.textContent = `Item ${i}`;
newItem.className = 'item';
fragment.appendChild(newItem);
}
// Appending the fragment to the container element
container.appendChild(fragment);
console.log('All items added to the container.');
});
/*
Explanation:
- The DOMContentLoaded event ensures that the script runs only after the DOM is fully loaded.
- A container element is selected where the new items will be added.
- A DocumentFragment is created to hold the new elements.
- A loop is used to create multiple div elements, which are then appended to the fragment.
- Finally, the fragment containing all the new elements is appended to the container element, efficiently updating the DOM in a single operation.
*/
/*
HTML to test with this script:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Example createDocumentFragment</title>
<style>
#container {
padding: 20px;
background-color: #f0f0f0;
}
.item {
background-color: #c0c0c0;
margin: 5px;
padding: 10px;
border: 1px solid #000;
}
</style>
</head>
<body>
<div id="container">Container</div>
<script src="example_create_document_fragment.js"></script>
</body>
</html>
*/