라라리라
2023.09.15 / Step 6 [DOM_이벤트응용] - 코딩 53 일차 본문
_07_노드1차원배열_그리기_이벤트적용.html
<!DOCTYPE html>
<html lang="ko">
<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>
td {
border: 1px solid black;
width: 40px;
height: 40px;
text-align: center; /* 가로 중앙정렬 */
}
</style>
</head>
<body>
<script>
// tdList = [td,td,td,td,td]
let clickEvent = function(e) {
for(let i=0; i<tdList.length; i++) {
if(e.target.innerText == tdList[i].innerText) {
alert(i);
}
}
}
let body = document.body;
let table = document.createElement("table");
let tr = document.createElement("tr");
let tdList = [];
for(let i=0; i<5; i++) {
let td = document.createElement("td");
td.addEventListener("click", clickEvent);
td.innerText = i + 1;
tr.append(td);
tdList.push(td);
}
table.append(tr);
body.append(table);
</script>
</body>
</html>
_08_노드여러개_그리기2_이벤트적용.html
<!DOCTYPE html>
<html lang="ko">
<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>
td {
border: 1px solid black;
width: 40px;
height: 40px;
text-align: center; /* 가로 중앙정렬 */
}
</style>
</head>
<body>
<script>
let num = 1;
let clickEvent = function(e) {
console.log(e);
e.target.innerText = num;
num += 1;
}
let body = document.body;
let table = document.createElement("table");
for(let i=0; i<5; i++) {
let tr = document.createElement("tr");
for(let j=0; j<5; j++) {
let td = document.createElement("td");
td.addEventListener("click", clickEvent);
tr.append(td);
}
table.append(tr);
}
body.append(table);
</script>
</body>
</html>
_09_노드2차원배열_그리기_이벤트적용.html
<!DOCTYPE html>
<html lang="ko">
<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>
td {
border: 1px solid black;
width: 40px;
height: 40px;
text-align: center; /* 가로 중앙정렬 */
}
</style>
</head>
<body>
<script>
let clickEvent = function(e) {
for(let i=0; i<list.length; i++) {
for(let j=0; j<list[i].length; j++) {
if(e.target.innerText == list[i][j].innerText) {
alert(i + ", " + j);
}
}
}
}
let body = document.body;
let table = document.createElement("table");
let num = 1;
let list = [];
for(let i=0; i<5; i++) {
let tdList = [];
let tr = document.createElement("tr");
for(let j=0; j<5; j++) {
let td = document.createElement("td");
td.addEventListener("click", clickEvent);
td.innerText = num;
tr.append(td);
num += 1;
tdList.push(td);
}
list.push(tdList);
table.append(tr);
}
body.append(table);
</script>
</body>
</html>
_10_화살표함수.html
<!DOCTYPE html>
<html lang="ko">
<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>
td {
border: 1px solid black;
width: 40px;
height: 40px;
text-align: center;
line-height: 40px;
}
</style>
</head>
<body>
<script>
/*
let num = 1;
function nodeEvent(e) {
e.target.innerText = num;
num += 1;
}
let num = 1;
let nodeEvent = function(e) {
e.target.innerText = num;
num += 1;
}
*/
let num = 1;
let nodeEvent = (e) => {
e.target.innerText = num;
num += 1;
}
let body = document.body;
let node = document.createElement("td");
node.addEventListener("click", nodeEvent);
body.append(node);
</script>
</body>
</html>
_11_변수명표기법.html
<!DOCTYPE html>
<html lang="ko">
<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>
td {
border: 1px solid black;
width: 40px;
height: 40px;
text-align: center;
line-height: 40px;
}
</style>
</head>
<body>
<script>
let num = 1;
let nodeEvent = (e) => {
e.target.innerText = num;
num += 1;
}
let $body = document.body;
let $node = document.createElement("td");
$node.addEventListener("click", nodeEvent);
$body.append($node);
</script>
</body>
</html>
'코딩 > 2023 JavaScript DOM' 카테고리의 다른 글
2023.09.19 / Step 6 [DOM_틱택토2단계] - 코딩 55 일차 (0) | 2023.09.19 |
---|---|
2023.09.18 / Step 6 [DOM_틱택토] - 코딩 54 일차 (0) | 2023.09.18 |
2023.09.14 / Step 6 [DOM_이벤트응용] - 코딩 52 일차 (0) | 2023.09.14 |
2023.09.13 / Step 5 [DOM_비동기] - 코딩 51 일차 (0) | 2023.09.13 |
2023.09.12 / Step 5 [DOM_비동기] - 코딩 50 일차 (0) | 2023.09.12 |