-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag.html
77 lines (58 loc) · 1.94 KB
/
tag.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
66
67
68
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="tag-container">
<input type="text" id="tag-input" placeholder=" 태그를 입력하세요.">
</div>
<!--css-->
<style>
#tag-input{
background-color: transparent;
border: none;
border-radius: 15px;
/*class 말고 id 꾸미기*/
}
.tag {
display: inline-block;
padding: 5px 10px;
margin: 5px;
border-radius: 12px;
cursor: pointer;
background-color: #ddd;
color: #008000;
font-weight: bold
}
</style>
<!--javascript-->
<script>
const tagContainer = document.getElementById("tag-container");
const tagInput = document.getElementById("tag-input");
const tagSet = new Set(); /*중복된 태그 확인*/
tagInput.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
const tagText = tagInput.value.trim(); /*입력창에서 Enter 키를 누르면 새 태그 생성*/
if (tagText && !tagSet.has(tagText)) {
createTag(tagText);
tagSet.add(tagText);
tagInput.value = ""; /*중복 검사*/
}
}
});
function createTag(tagText) {
const tagElement = document.createElement("div");
tagElement.classList.add("tag");
tagElement.textContent = tagText;
tagContainer.appendChild(tagElement);
tagElement.addEventListener("click", () => {
tagContainer.removeChild(tagElement);
tagSet.delete(tagText);
});/*태그 삭제*/
}
</script>
</body>
</html>