-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfive.html
More file actions
80 lines (75 loc) · 1.92 KB
/
five.html
File metadata and controls
80 lines (75 loc) · 1.92 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
78
79
80
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DOM -> child and parent relation</title>
<style>
.container {
display: flex;
gap: 2rem;
flex-direction: column;
}
button {
width: 100px;
background: #E4E5E9;
border: none;
width: 8rem;
height: 2rem;
font-size: 16px;
box-shadow: 3px 2px 3px black;
}
button:hover{
width: 9rem;
height: 2.5rem;
transition: ease-in-out 0.3s;
background-color: rgb(66, 133, 244);
box-shadow: 3px 2px 3px rgba(0, 0, 0, 0.797);
color: white;
}
div.parent {
border: 1px solid black;
padding: 5px;
width: 100px;
height: 100px;
}
div.child {
border: 1px solid red;
margin: 10px;
padding: 5px;
width: 80px;
height: 60px;
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="container">
<div class="parent">parent</div>
<button id="add-child" type="button">Add a child</button>
<button id="remove-child" type="button">Remove child</button>
</div>
</body>
<script>
// how to add-child and how to remove-child it
const parent = document.body.querySelector(".parent");
const addChild = document.body.querySelector("#add-child");
addChild.addEventListener("click", ()=>{
// Only add a child if we don't already have one
// in addition to the text node "parent"
if (parent.childNodes.length > 1) {
return;
}
const child = document.createElement("div");
child.classList.add("child");
child.textContent = "i am child";
parent.appendChild(child);
});
// for remove child
const removeChild = document.body.querySelector("#remove-child");
removeChild.addEventListener("click", () =>{
const child = document.body.querySelector(".child");
parent.removeChild(child);
});
</script>
</html>