<style>
.parent{
width:300px;
height:200px;
padding:20px;
border:10px solid;
background-color:red;
overflow:auto;
}
.child{
width:200px;
height:1000px;
border:10px solid;
background-color:orange;
font-size:40px;
}
</style>
<body>
<div class="parent">
<div class="child">
<a href="https://naver.com" target="_blank">naver</a>
</div>
</div>
<input />
</body>
const parentEl = document.querySelector('.parent')
const childEl = document.querySelector('.child')
parentEl.addEventListener('click',()=>{
console.log('Parent')
})
childEl.addEventListener('click',()=>{
console.log('Child')
})
const parentEl = document.querySelector('.parent')
const childEl = document.querySelector('.child')
const handler = () => {
console.log('Parent')
}
parentEl.addEventListener('click',handler)
childEl.addEventListener('click',()=>{
parentEl.removeEventListener('click',handler)
})
const parentEl = document.querySelector('.parent')
parentEl.addEventListener('click',event => {
console.log(event.target , event.currentTarget);
//클릭한 요소 , 이벤트가 등록된 요소
})
parentEl.addEventListener('wheel',event => {
console.log(event);
})
const inputEl = document.querySelector('input')
inputEl.addEventListener('keydown',event => {
console.log(event.key);
})
const parentEl = document.querySelector('.parent')
parentEl.addEventListener('wheel',event => {
event.preventDefault();
console.log(event);
})
<a>
태그에서 페이지 이동 방지!const anchorEl = document.querySelector('a')
anchorEl.addEventListener('click',event => {
console.log(event);
event.preventDefault();
})
이벤트는 후손요소로 부터 상위요소로 전파되는 특성이 있다.
const parentEl = document.querySelector('.parent')
const childEl = document.querySelector('.child')
const anchorEl = document.querySelector('a')
window.addEventListener('click',event=>{
console.log('Window!')
})
document.body.addEventListener('click',event=>{
console.log('Body!')
})
parentEl.addEventListener('click',event=>{
console.log('Parent!')
event.stopPropagation(); //버블링 정지
})
childEl.addEventListener('click',event=>{
console.log('Child!')
})
anchorEl.addEventListener('click',event=>{
console.log('Anchor!')
})
a태그를 선택하게 되면 후손요소에서 상위요소로 부터 전파되어 출력된다.
event.stopPropagation() 메서드를사용하게 되면 Parent 에서 부터 상위로 전파되는것을 막았기 때문에 Parent까지만 console에 표출된다.
const parentEl = document.querySelector('.parent')
const childEl = document.querySelector('.child')
const anchorEl = document.querySelector('a')
window.addEventListener('click',event=>{
console.log('Window!')
})
document.body.addEventListener('click',event=>{
console.log('Body!')
},{capture:true})
parentEl.addEventListener('click',event=>{
console.log('Parent!')
})
childEl.addEventListener('click',event=>{
console.log('Child!')
})
anchorEl.addEventListener('click',event=>{
console.log('Anchor!')
})
const parentEl = document.querySelector('.parent')
const childEl = document.querySelector('.child')
const anchorEl = document.querySelector('a')
window.addEventListener('click',event=>{
console.log('Window!')
})
document.body.addEventListener('click',event=>{
console.log('Body!')
event.stopPropagation();
},{capture:true})
parentEl.addEventListener('click',event=>{
console.log('Parent!')
},{capture:true})
childEl.addEventListener('click',event=>{
console.log('Child!')
})
anchorEl.addEventListener('click',event=>{
console.log('Anchor!')
})
const parentEl = document.querySelector('.parent')
const childEl = document.querySelector('.child')
const anchorEl = document.querySelector('a')
window.addEventListener('click',event=>{
console.log('Window!')
},{capture:true})
document.body.addEventListener('click',event=>{
console.log('Body!')
event.stopPropagation(); //이벤트 전파 정지
},{capture:true})
parentEl.addEventListener('click',event=>{
console.log('Parent!')
},{capture:true})
childEl.addEventListener('click',event=>{
console.log('Child!')
})
anchorEl.addEventListener('click',event=>{
console.log('Anchor!')
})
const parentEl = document.querySelector('.parent')
const handler = () => {
console.log('Parent!')
}
parentEl.addEventListener('click',handler,{capture:true})
parentEl.removeEventListener('click',handler,{capture:true})
const parentEl = document.querySelector('.parent')
const handler = () => {
console.log('Parent!')
}
parentEl.addEventListener('click',()=>{
console.log('Parent!')
},{
once:true
})
const parentEl = document.querySelector('.parent')
parentEl.addEventListener('wheel',()=>{
for(let i=0;i<10000;i++){
console.log(i)
}
},{
passive:true
})
<div class="parent">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div>
</div>
const parentEl = document.querySelector('.parent')
const childEls = document.querySelectorAll('.child')//nodeList 반환
//모든 대상 요소에 이벤트 등록!
childEls.forEach(el => {
el.addEventListener('click',event => {
console.log(event.target.textCotent);
})
})
//조상 요소에 이벤트 위임
parentEl.addEventListener('click',event => {
const childEl = event.target.closest('.child')
if(childEl){
console.log(childEl.textCotent);
}
})
click
: 클릭했을때
dblclick
: 더블 클릭했을때
mousedown
: 버튼을 누를때
mouseup
: 버튼을 뗄 때
mouseenter
: 포인터가 요소 위로 들어갈 때
mouseleave
: 마우스를 요소 밖으로 나올 때
mousemove
: 포인터가 움직일 때
contextmenu
: 우클릭했을 때
wheel
: 휠 버튼이 회전할 때
<style>
.parent{
width:300px;
height:200px;
padding:20px;
border:10px solid;
background-color:red;
overflow:auto;
}
.child{
width:200px;
height:1000px;
border:10px solid;
background-color:orange;
}
.child.active{
background-color:yellowgreen;
}
</style>
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>
const parentEl = document.querySelector('.parent')
const childEl = document.querySelector('.child')
childEl.addEventListener('mousedown',()=>{
childEl.classList.toggle('active')
})
childEl.addEventListener('mouseup',()=>{
childEl.classList.add('active')
})
childEl.addEventListener('mouseup',()=>{
childEl.classList.remove('active')
})
keydown
: 키를 누를때
keyup
: 키를 땔 때
<body>
<input />
</body>
const inputEl = document.querySelector('input');
inputEl.addEventListener('keydown',event => {
if(event.key === 'Enter'){
console.log(event.isComposing)
console.log(event.target.value)
}
})
const inputEl = document.querySelector('input');
inputEl.addEventListener('keydown',event => {
if(event.key === 'Enter' && !event.isComposing){
console.log(event.isComposing)
console.log(event.target.value)
}
})
focus
: 요소가 포커스를 얻었을때
blur
: 요소가 포커스를 잃었을때
input
: 값이 변경되었을때
change
: 상태가 변경되었을때
submit
: 제출 버튼을 선택했을때
reset
: 리셋 버튼을 선택했을때
<style>
form{
padding:10px;
border:4px solid transparent;
display:flex;
flex-wrap:wrap;
gap:6px;
}
form.active{
border-color:orange;
}
</style>
<body>
<form>
<input type="text" placeholder="ID" />
<input type="password" placeholder="PW" />
<button type="submit">제출</button>
<button type="reset">초기화</button>
</form>
</body>
const formEl = document.querySelector('form')
const inputEls = document.querySelectorAll('input')
inputEls.forEach(el => {
el.addEventListener('focus',()=>{
formEl.classList.add('active')
})
el.addEventListener('blur',()=>{
formEl.classList.remove('active')
})
el.addEventListener('input',event=>{
console.log(event.target.value)
})
})
formEl.addEventListener('submit',event => {
event.preventDefault()
const data = {
id:event.target[0].value,
pw:event.target[1].value,
}
console.log('제출!',data)
})
formEl.addEventListener('reset',event => {
console.log('리셋')
})
<div class="parent">
<div class="child"></div>
<div class="child"></div>
<div>
const child1 = document.querySelector('.child:nth-child(1)')
const child2 = document.querySelector('.child:nth-child(2)')
child1.addEventListener('click',event => {
child2.dispatchEvent(new Event('click'));
child2.dispatchEvent(new Event('wheel'));
child2.dispatchEvent(new Event('keydown'));
})
child2.addEventListener('click', event => {
console.log('Child2 Click')
})
child2.addEventListener('wheel', event => {
console.log('Child2 Wheel')
})
child2.addEventListener('keydown', event => {
console.log('Child2 Keydown')
})
const child1 = document.querySelector('.child:nth-child(1)')
const child2 = document.querySelector('.child:nth-child(2)')
child1.addEventListener('hello-world',event => {
console.log('커스텀 이벤트 발생!')
console.log(event.detail)
})
child2.addEventListener('click', event => {
child1.dispatchEvent(new Event('hello-world'));
})
const child1 = document.querySelector('.child:nth-child(1)')
const child2 = document.querySelector('.child:nth-child(2)')
child1.addEventListener('hello-world',event => {
console.log('커스텀 이벤트 발생!')
console.log(event.detail)
})
child2.addEventListener('click', event => {
child1.dispatchEvent(new CustomEvent('hello-world',{
detail:123
}));
})