-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclas-list.html
56 lines (49 loc) · 1.79 KB
/
clas-list.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>classList Example</title>
<style>
.highlight {
color: red;
font-weight: bold;
}
.italic {
font-style: italic;
}
.underline {
text-decoration: underline;
}
.strike {
text-decoration: line-through;
}
</style>
</head>
<body>
<p id="myParagraph" class="highlight">This is a paragraph.</p>
<button onclick="performClassListOperations()">Perform Operations</button>
<script>
function performClassListOperations() {
// Getting the Paragraph Element
const paragraph = document.getElementById("myParagraph");
// Adds the class "italic" to the paragraph element's class list.
paragraph.classList.add("italic");
// Removes the class "highlight" from the paragraph element's class list.
paragraph.classList.remove("highlight");
// Toggles the class "underline" on the paragraph element. In this case, it explicitly adds the class "underline" because the second parameter is true.
paragraph.classList.toggle("underline", true);
// Checking if a class exists
const hasItalicClass = paragraph.classList.contains("italic");
console.log(`Has italic class: ${hasItalicClass}`);
// Replacing a class after a delay (for demonstration)
setTimeout(() => {
paragraph.classList.replace("underline", "strike");
// Accessing classes as a string
const classString = paragraph.classList.toString();
console.log(`Current classes: ${classString}`);
}, 2000); // Delay for 2 seconds
}
</script>
</body>
</html>