동적으로 html 생성 후 이벤트 위임 질문 있습니다.
266
3 câu hỏi đã được viết
안녕하세요. 이벤트 위임 연습하다가 변칙적으로 연습하고 있는데요. 동적으로 html 생성 된 후에 버튼에 ''-active"클래스 추가 하면 실제로 클래스가 추가가 안되네요. 그런데 elem을 consol 창에 찍어보면 "-avtive"클래스가 추가된 요소로 나오는데 이건 무슨 문제일까요?

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>스크립트 연습</title>
<style>
*, *::before, *::after {margin:0; padding:0; box-sizing:border-box;}
h1 {padding:20px 0;}
h2 {padding-bottom:20px;}
li {list-style:none;}
.container {max-width:1000px; margin:0 auto; padding:0 20px; background-color:#f1f1f1;}
.wrap {padding:40px; border:1px solid #888;}
.wrap + .wrap {margin-top:50px;}
.btn-list {display:flex; justify-content:space-between; gap:20px; width:100%; padding:20px; background-color:dodgerblue;}
.btn-list li {width:calc(100% / 3);}
.btn-list__item {width:100%; padding:10px;}
.btn-list__item.-active {background-color:darkkhaki;}
</style>
</head>
<body>
<div class="container">
<h1>스크립트 연습</h1>
<section class="wrap btn-wrap">
<h2>버튼 연습</h2>
<ul class="btn-list">
<!-- <li class="asdf"><button class="btn-list__item"><span>버튼</span> 1버튼</button></li>
<li class="asdf"><button class="btn-list__item"><span>버튼</span> 2버튼</button></li>
<li class="asdf"><button class="btn-list__item"><span>버튼</span> 3버튼</button></li> -->
</ul>
</section>
<script>
window.addEventListener('DOMContentLoaded', initHandler)
function initHandler() {
buttonListHandler();
}
function buttonListHandler() {
const btnWrap = document.querySelector('.btn-wrap');
const btnList = document.querySelector('.btn-list');
let currentItem = null;
function clickHandler(el) {
let elem = el.target;
while (!elem.classList.contains('btn-list__item')){
elem = elem.parentNode;
// console.log(elem)
if(elem.nodeName === 'BODY'){
elem = null;
return;
}
}
if(currentItem){
currentItem.classList.remove('-active');
}
if(elem.classList.contains('btn-list__item')){
elem.classList.add('-active');
currentItem = elem;
}
console.log(elem);
}
btnWrap.addEventListener('click', ()=> {
const htmlStr = `
<li><button class="btn-list__item"><span>버튼</span> 1버튼</button></li>
<li><button class="btn-list__item"><span>버튼</span> 2버튼</button></li>
<li><button class="btn-list__item"><span>버튼</span> 3버튼</button></li>
`;
btnList.innerHTML = htmlStr;
})
btnWrap.addEventListener('click', clickHandler);
}
</script>
</div>
</body>
</html>
Câu trả lời 1
0
html을 생성하는 버튼을 추가하니까 되네요.
그런데 윈도우나 박스를 클릭해서 html생성한 후에 이벤트는 왜 안먹는지 그건 잘 모르겠네요. 그럴 일도 별로 없겠지만요...
function innerHtml() {
const addButton = document.querySelector('.add-buttons');
btnWrap.addEventListener('click', ()=> {
const htmlStr = `
<li><button class="btn-list__item"><span>버튼</span> 1버튼</button></li>
<li><button class="btn-list__item"><span>버튼</span> 2버튼</button></li>
<li><button class="btn-list__item"><span>버튼</span> 3버튼</button></li>
`;
btnList.innerHTML = htmlStr;
})
}
innerHtml();
왼쪽/오른쪽 동작시 딜레이 문제
0
93
1
변수 범위 관련 질문
0
106
1
perspective 문의
0
101
1
생성자 함수를 클래스 함수로 변경 하고 this 오류 관련
0
150
1
스크롤이 중간 위치에 있을 때 창의 크기를 변환하면 생기는 문제
0
127
1
animation이벤트 질문이요!
0
71
1
resize handler에서 질문이 있습니다.
0
111
1
카드 뒤집힐 때 F가 보인 이유
0
149
1
3d 뒤집기 추가효과
0
218
1
전진! 3D 스크롤 21 강의 질문
1
171
1
eventlistener 질문
0
149
1
zMove 를 1000으로 설정하는 이유에 대하여.
0
168
1
[정보-23강] ES6 class 문법으로 공부하시는 분들!! 화살표 함수로도 시도해보셔요!
1
191
1
rotateY()에서 deg에 따른 차이
0
197
1
코드 작성 순서
0
276
1
이미지가 없는데 첨부파일을 다운 받는 방법이 있나요??
1
394
1
'이벤트 위임 보강 영상'에 있는 예제 html이 안 보입니다
0
265
2
섹션5 자바스크립트 이벤트 다루기 질문
1
243
1
[#전진! 3D 스크롤 11] mousePos 공식 질문 있습니다!
0
423
2
css 는 직접 작성을 해야하는걸까용?
0
326
1
translateZ 에 px 이 아닌 vw 로 값을 주신 이유가 있을가요?
0
361
2
house 부분에도 width , height 부분을 꽉 차게 주신 부분이 제가 이해한게 맞는지 궁금합니다.
0
305
2
left:-400vw 가 아닌 translateZ(100vw); 을 입력하신 이유가 궁금합니다.
0
306
2
외부에서 JS파일을 불러올땐 무조건 defer를 써도 괜찮은건가요?
0
304
2

