-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJS
More file actions
77 lines (70 loc) · 1.97 KB
/
Copy pathJS
File metadata and controls
77 lines (70 loc) · 1.97 KB
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
69
70
71
72
73
74
75
76
77
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DOM Interaction Example</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
background-color: #f0f4f8;
}
#dynamicText {
color: #0077cc;
font-size: 1.2rem;
margin-bottom: 15px;
}
.highlight {
background-color: yellow;
padding: 5px;
border-radius: 4px;
}
</style>
</head>
<body>
<header>
<h1>JavaScript DOM Manipulation</h1>
</header>
<main>
<section>
<p id="dynamicText">Click the button to change this text and style.</p>
<button onclick="changeText()">Change Text & Style</button>
</section>
<section style="margin-top: 20px;">
<button onclick="addItem()">Add Item</button>
<button onclick="removeItem()">Remove Item</button>
<ul id="itemList">
<li>Item 1</li>
<li>Item 2</li>
</ul>
</section>
</main>
<footer style="margin-top: 40px;">
<p>© 2025 DOM Tutorial. All rights reserved.</p>
</footer>
<!-- Link JavaScript File -->
<script src="script.js"></script>
</body>
</html>
script.js — DOM Interaction Logic
javascript
Copy
Edit
function changeText() {
const text = document.getElementById('dynamicText');
text.textContent = 'Text has been changed! ';
text.classList.toggle('highlight');
}
function addItem() {
const ul = document.getElementById('itemList');
const newItem = document.createElement('li');
newItem.textContent = `Item ${ul.children.length + 1}`;
ul.appendChild(newItem);
}
function removeItem() {
const ul = document.getElementById('itemList');
if (ul.children.length > 0) {
ul.removeChild(ul.lastElementChild);
}
}