keyEvent2.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div></div>
<script>
document.addEventListener('keydown', function(e){
if(e.key === 'Enter') alert('안녕하세요');
})
</script>
</body>
</html>
keyEvent_ex.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
div {
width: 100%;
height: 100vh;
background-color:lightsteelblue;
display: flex;
justify-content: center;
align-items: center;
}
ul {
display: flex;
}
li {
list-style: none;
width: 120px;
height: 120px;
border-radius: 10%;
background-color: #3e3e3e;
margin: 6px;
color: #fff;
text-align: center;
line-height: 120px;
font-size: 38px;
transition: 0.5s;
}
li.on {
background-color:midnightblue;
color: yellow;
transform: scale(1.2);
}
</style>
</head>
<body>
<div>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</div>
<script>
let lis = document.querySelectorAll('li');
document.addEventListener('keydown', function(e){
lis[Number(e.key)-1].classList.add('on');
})
document.addEventListener('keyup', function(e){
lis[Number(e.key)-1].classList.remove('on');
})
</script>
</body>
</html>
todolist_t.html (Enter / Delete 기능 추가 버전)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<title>Document</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
li { list-style: none; }
#wrap {
width: 100%;
max-width: 1000px;
margin: 0 auto;
}
#todoheader {
color: #fff;
padding: 30px 0;
text-align: center;
/* linear-grdient(방향, 색상1, 색상2, 색상3, ...) */
background-image: linear-gradient(-20deg, #dcb0ed 0%, #99c99c 100%);
height: 150px;
}
#todoheader input {
border: none;
width: 60%;
height: 40px;
border-radius: 20px;
background-color: #fff;
padding-left: 30px;
outline: none;
}
#todoheader button {
border: none;
/* outline: none; */
font-size: 40px;
background: transparent; /* transparent: 투명 */
}
#todoheader button i {
font-size: 40px;
color: #fff;
}
#insertBtn {
height: 40px;
vertical-align: middle;
}
#listUl li {
background: #eee;
line-height: 40px;
padding-left: 20%;
padding-right: 20%;
position: relative;
}
#listUl li span {
float: right;
}
#listUl li.check {
text-decoration: line-through;
background: #ddd;
}
#listUl li.check::before {
content: "";
display: block;
width: 30px;
height: 15px;
border-bottom: 1px solid #333;
border-left: 1px solid #333;
transform: rotate(320deg);
position: absolute;
left: 16%;
top: 5px;
}
</style>
<script defer src="todoScript.js"></script>
</head>
<body>
<div id="wrap">
<div id="todoheader">
<h1>to do list</h1>
<div>
<input type="text" id="todoInput">
<button id="insertBtn"><i class="material-icons">add_circle</i></button>
</div>
</div>
<div id="todolist">
<ul id="listUl"></ul>
</div>
</div>
</body>
</html>
todoScript.js (Enter / Delete 기능 추가 버전)
// 변수 선언 btn input ul
let btn = document.querySelector('#insertBtn');
let input = document.querySelector('#todoInput');
let ul = document.querySelector('#listUl');
btn.addEventListener('click', addList)
input.addEventListener('keydown',function(e){
console.log(e.key);
if(e.key === 'Enter') addList();
})
// btn 클릭시 실행되는 함수
// input의 value가 있는 지 확인, 없으면 return, 있으면 그 값을
// li에 넣어주고 li를 ul에 추가
function addList() {
if(!input.value) return;
let li = document.createElement('li');
li.innerHTML=`${input.value}<span>X</span>`
ul.append(li);
input.value = '';
removeEvent();
li.addEventListener('click', function(){
document.addEventListener('keydown', function(e){
if(e.key === 'Delete') li.remove();
})
})
}
// x를 클릭했을 때 실행되는 함수
// 클릭한 x의 부모요소를 삭제하기
function removeEvent(){
let spans = document.querySelectorAll('#listUl span')
spans.forEach(span => span.addEventListener('click', function(){
this.parentElement.remove();
}))
}
// ul을 클릭하면 클릭한 대상이 li면 check 클래스를 지정
// check 클래스가 있으면 제거
ul.addEventListener('click', function(e){
console.log(e);
if(e.target.nodeName === 'LI') e.target.classList.toggle('check');
})
// 엔터 누를시 추가
// document.addEventListener('keydown', function(e){
// if(e.key === 'Enter') addList();
// })
'Stack > JavaScript' 카테고리의 다른 글
[JS] Daum 우편번호 서비스 API 활용 (0) | 2022.05.18 |
---|---|
[JS] Form태그 (0) | 2022.05.18 |
[JS] 쪽지시험 끄적끄적 (1) | 2022.05.16 |
[JS] Scroll (2) | 2022.05.16 |
[JS] 타자게임 (0) | 2022.05.16 |