</head>
<body>
<h3>Bubbling ๊ณผ Capturing</h3>
<!--
Bubbling : ํ์ ์์์์ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ฉด ํ์์์ ์์๋ก ์ด๋ฒคํธ๊ฐ ์ ํ(๊ธฐ๋ณธ)
Capturing : ํ์ ์์์์ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ฉด ์์์์ ํ์๋ก ์ด๋ฒคํธ๊ฐ ์ ํ
-->
<div class="box1">
DIV ์์ญ
<p class="box2">
p ์์ญ <span class="box3">SPAN์์ญ</span>
</p>
</div>
<hr>
<div class="console"></div>
<script type="text/javascript">
const box1 = document.querySelector('.box1');
const box2 = document.querySelector('.box2');
const box3 = document.querySelector('.box3');
const console = document.querySelector('.console');
//๊ธฐ๋ณธ :Bubbling
//Span ์ ํด๋ฆญํ๋ฉด : SPANํด๋ฆญ > Pํด๋ฆญ < DIV ํด๋ฆญ ์์ผ๋ก ์คํ
/*
box1.addEventListener('click', function(e) {
console.innerHTML += 'DIVํด๋ฆญ<br>';
});
box2.addEventListener('click', function(e) {
console.innerHTML += 'P ํด๋ฆญ<br>';
});
box3.addEventListener('click', function(e) {
console.innerHTML += 'SPAN ํด๋ฆญ <br>';
});
*/
//Capturing
//Span ์ ํด๋ฆญํ๋ฉด "DIVํด๋ฆญ > Pํด๋ฆญ > SPAN ํด๋ฆญ"
box1.addEventListener('click', function(e) {
console.innerHTML += 'DIVํด๋ฆญ<br>';
},true);
box2.addEventListener('click', function(e) {
console.innerHTML += 'P ํด๋ฆญ<br>';
},true);
box3.addEventListener('click', function(e) {
console.innerHTML += 'SPAN ํด๋ฆญ <br>';
},true);
</script>
</body>
</html>
</head>
<body>
<h3>ํค๋ณด๋ ๊ด๋ จ ์ด๋ฒคํธ</h3>
<p>
<input type="text" id="inputs">
</p>
<hr>
<div id="Layout"></div>
<hr>
<!--
- ASC II ํค : a~z, 0~9, ์ํฐ ๋ฑ
๋ฐ์๋๋ ์ด๋ฒคํธ : keydown -> keypress -> keyup
- ํน์ํค : SHift, Alt, ctrl , F1~F12 ๋ฑ
๋ฐ์๋๋ ์ด๋ฒคํธ : keydown - > keyup
- keypress : ์ค์ ๋๋ฌ์ง ํค์ฝ๋๋ก ๋ฐํ(์ฌ์ฉํ์ง ๋ง๊ฒ์ ๊ถ์ฅ)
key ์์ฑ์ผ๋ก ์ค์ ๋๋ฌ์ง ํค๋ฅผ ํ์ธ ํ ์ ์๋ค.
-->
<script type="text/javascript">
const inputEL = document.querySelector('#inputs');
const divEL = document.querySelector('#Layout');
//keydown ์ด๋ฒคํธ ๋ฑ๋ก ๋ฐ ์ด๋ฒคํธ ํธ๋ค๋ฌ
inputEL.onkeydown=e=>{
//keyCode ๋์ key ์ฌ์ฉ์ ๊ถ์ฅ
let keyCode = e.keyCode; // // aํค๋ฅผ ๋๋ฅด๋ฉด 65, Aํค๋ฅผ ๋๋ฅด๋ฉด 65 ๋ฐํ
let key = e.key; // aํค๋ฅผ ๋๋ฅด๋ฉด 'a', Aํค๋ฅผ ๋๋ฅด๋ฉด 'A' ๋ฐํ
let s = 'keydown : keyCode -' + keyCode +',key-'+key+'<br>';
divEL.innerHTML=s;
};
inputEL.onkeypress=e=>{
//keypress ์ด๋ฒคํธ๋ ์ฌ์ฉํ์ง ๋ง๊ฒ์ ๊ถ์ฅ
let keyCode = e.keyCode; // // aํค๋ฅผ ๋๋ฅด๋ฉด 97, Aํค๋ฅผ ๋๋ฅด๋ฉด 65 ๋ฐํ
let key = e.key; // aํค๋ฅผ ๋๋ฅด๋ฉด 'a', Aํค๋ฅผ ๋๋ฅด๋ฉด 'A' ๋ฐํ
let s = 'keypress : keyCode -' + keyCode +',key-'+key+'<br>';
divEL.innerHTML=s;
};
inputEL.onkeyup=e=>{
//keypress ์ด๋ฒคํธ๋ ์ฌ์ฉํ์ง ๋ง๊ฒ์ ๊ถ์ฅ
let keyCode = e.keyCode; // // aํค๋ฅผ ๋๋ฅด๋ฉด 65, Aํค๋ฅผ ๋๋ฅด๋ฉด 65 ๋ฐํ
let key = e.key; // aํค๋ฅผ ๋๋ฅด๋ฉด 'a', Aํค๋ฅผ ๋๋ฅด๋ฉด 'A' ๋ฐํ
let s = 'keyup : keyCode -' + keyCode +',key-'+key+'<br>';
divEL.innerHTML+=s;
};
</script>
</body>
</html>
</head>
<body>
<h3>ํค ์ด๋ฒคํธ</h3>
<p>
์ซ์๋ง : <input type="text" class="inputNum">
</p>
<script type="text/javascript">
const inputNumEL=document.querySelector('input.inputNum');
const onlyNumber = e =>{
/*let key = e.key || e.keyCode;
if((key==='Enter' && ! e.shiftKey)||(key===13 && key!==16)){
//Enter๋ฅผ ๋๋ฅธ ๊ฒฝ์ฐ(Shift ํค๋ฅผ ๋๋ฅด์ง ์์ ์ํ)
}
*/
let key = e.key;
//keypress ์ด๋ฒคํธ๋ก ์ฒ๋ฆฌํ๋ฉด Backspace๋ฑ์ ๋๋ฌ๋ keypress ์ด๋ฒคํธ๊ฐ ๋ฐ์๋์ง
//์์ผ๋ฏ๋ก ์๋ ์ฝ๋๋ฅผ ์์ฑํ์ง ์์๋ ๋๋ค.
if(key==='Backspace' || key ==='Tab' || key ==='ArrowLeft' || key ==='ArrowRight' || key === 'Delete'){
return;
}
if(key<'0'||key>'9'){
e.preventDefault();
}
};
inputNumEL.addEventListener('keydown',e=> onlyNumber(e))
</script>
<p>
์๋ฌธ์๋ง : <input type="text" class="inputAlph">
</p>
<script type="text/javascript">
const inputAlphEl = document.querySelector('input.inputAlph');
const onlyAlphabet = e =>{
let key = e.key;
if(key==='Backspace' || key ==='Tab' || key ==='ArrowLeft' || key ==='ArrowRight' || key === 'Delete'){
return;
}
if((key<'A'||key>'Z')&&(key<'a'||key>'z')){
e.preventDefault();
}
};
inputAlphEl.addEventListener('keydown',e=>onlyAlphabet(e));
</script>
<p>
์์ซ์ : <input type="text" class="inputAlphNum">
</p>
<script type="text/javascript">
const inputAlphNumEL = document.querySelector('input.inputAlphNum');
const onlyAlphNum = e =>{
let key = e.key;
if(key==='Backspace' || key ==='Tab' || key ==='ArrowLeft' || key ==='ArrowRight' || key === 'Delete'){
return;
};
if(!/^\w{1}$/.test(key)){
e.preventDefault();
}
};
// //inputAlphNumEL.addEventListener('keydown',e=>onlyAlphNum(e));
inputAlphNumEL.addEventListener('keydown',e=>onlyAlphNum(e));
</script>
<p>
์
๋ ฅ๋ ํ๊ธ ์ญ์ : <input type="text" class="inputDeleteKor">
</p>
<script type="text/javascript">
const inputDeleteKorEL=document.querySelector('input.inputDeleteKor');
const deleteKorean=e=>{
target.value=target.value.replace(/[ใฑ-ใ
ใ
-ใ
ฃ ๊ฐ-ํฃ]/g,'');
};
inputDeleteKorEL.addEventListener('keyup',e=>deleteKoeran(e));
</script>
<p>
ํ๊ธ๋ง ์
๋ ฅ : <input type="text" class="inputKor">
</p>
<script type="text/javascript">
const inputKorEL=document.querySelector('input.inputKor');
const onlyKorean =e=>{
let keyCode = e.keyCode;
// ํ๊ธ์ e.key๋ก ํ์ธ ๋ถ๊ฐ
if( keyCode<12592 || keyCode>12687){
e.preventDefault();
}
};
inputKorEL.addEventListener('keypress',e=>onlyKorean(e));
</script>
<p>
ํ๊ธ์
๋ ฅ ์ ํ : <input type="text" class="inputNoKor">
</p>
<script type="text/javascript">
const inputNoKorEL = document.querySelector('input.inputNoKor');
//compositionstart ์ด๋ฒคํธ : ํ๊ธ๋ฑ ์กฐํฉ ๋ฌธ์๋ฅผ ์
๋ ฅํ ๋ ๋ฐ์ํ๋ ์ด๋ฒคํธ
inputNoKorEL.addEventListener('compositionstart',e=>{
const self = e.currentTarget;
self.blur();
//๋ค์ ๋ฆฌํ์ดํธ์ ์คํ
requestAnimationFrame(()=> self.focus());
});
</script>
</body>
</html>
</head>
<body>
<p>ํ๊ธ ์
๋ ฅ ์ ํ</p>
<p>ํ๊ธ ๊ฐ๋ฅ : <input type="text"></p>
<p>ํ๊ธ ์ ํ : <input type="text" class="notKorean"></p>
<p>ํ๊ธ ์ ํ : <input type="text" class="notKorean"></p>
<p>ํ๊ธ ์ ํ : <input type="text" class="notKorean"></p>
<p>ํ๊ธ ๊ฐ๋ฅ : <input type="text"></p>
<script type="text/javascript">
const inputELements = document.querySelectorAll('input.notKorean');
//์๋ฌธ ์
๋ ฅํ ํ๊ธ ์
๋ ฅํ๋ฉด ์์ ์๋ฌธ์ด ์ญ์ ๋ ์ ์์
const notKorean =e=>{
const target = e.currentTarget;
//target.value=target.value.replace(/^[\W]$/g,'');
target.value=target.value.replace(/[ใฑ-ใ
ใ
-ใ
ฃ ๊ฐ-ํฃ]/g,'');
};
inputELements.forEach(inputEL=>inputEL.addEventListener('input',e=>notKorean(e)));
</script>
</body>
</html>
</head>
<body>
<h3>F12, ๋ง์ฐ์ค ์ฐ์ธก</h3>
<script type="text/javascript">
window.addEventListener('load',()=>{
document.addEventListener('keydown',e=>{
if(e.key==='F12'){
alert('๊ฐ๋ฐ์ ๋๊ตฌ ๋ง์...')
e.preventDefault();
}
});
document.addEventListener('mousedown',e=>{
if(e.button===2||e.button===3){
alert('๋ง์ฐ์ค ์ฐ์ธก ๋๋ฅด๋ฉด ์ค๋ ๊ณผ์ ์๋ค.');
}
});
});
</script>
</body>
</html>
<style type="text/css">
.box {
width: 150px;
height: 150px;
border: 1px solid black;
}
</style>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<h3>๋ง์ฐ์ค ๊ด๋ จ ์ด๋ฒคํธ</h3>
<div class="box"></div>
<hr>
<div class=:console></div>
<script type="text/javascript">
const boxEL = document.querySelector('.box');
const consoleEL = document.querySelector('.console');
boxEL.addEventListener('mouseover',e=>{
//๋ง์ฐ์ค ํฌ์ธํฐ๊ฐ ๊ฐ์ฒด ์๋ก ์ฌ๋ผ์ฌ๋
e.currentTarget.style.backgroundColor='#ff0';
consoleEL.innerHTML=`mouseover:<br>`;
});
boxEL.addEventListener('mouseout',e=>{
//๋ง์ฐ์ค ํฌ์ธํฐ๊ฐ ๊ฐ์ฒด๋ฅผ ๋ฒ์ด๋ ๋
consoleEL.innerHTML+=`mouseout:<br>`;
});
boxEL.addEventListener('mousedown',e=>{
//๋ง์ฐ์ค๋ฅผ ๋๋ฅผ๋
consoleEL.innerHTML+=`mousedown:<br>`;
});
boxEL.addEventListener('mouseup',e=>{
//๋ง์ฐ์ค๋ฅผ ๋๋ ๋ค ๋์๋
consoleEL.innerHTML+=`mouseup:<br>`;
});
boxEL.addEventListener('dblclick',e=>{
//๊ฐ์ฒด๋ฅผ ๋๋ธํด๋ฆญํ ๋
consoleEL.innerHTML+=`dblclick:<br>`;
});
</script>
</body>
</html>
<style type="text/css">
.box {
width: 300px;
height: 700px;
border: 3px dotted gray;
}
</style>
</head>
<body>
<h3>๋ง์ฐ์ค ํด๋ฆญ ์ขํ</h3>
<div class="box"></div>
<script type="text/javascript">
const boxEL = document.querySelector('div.box');
boxEL.addEventListener('click',e=>{
//document์ ๊ฐ์ฅ ์ผ์ชฝ ์ง์ ๋ถํฐ์ x,y ์ขํ(์คํฌ๋กค๋ฐ ์ ์ธ)
let x = e.clientX;
let y = e.clientY;
boxEL.innerHTML = `client ์ขํ: (${x},${y})<br>`;
let x2 = e.pageX;
let y2 = e.pageY;
boxEL.innerHTML += `page ์ขํ: (${x2},${y2})<br>`;
let x3 = e.offsetX;
let y3 = e.offsetY;
boxEL.innerHTML += `offset ์ขํ: (${x3},${y3})<br>`;
let x4 = e.screenX;
let y4 = e.screenY;
boxEL.innerHTML += `screen ์ขํ: (${x4},${y4})<br>`;
});
</script>
</body>
</html>
<style type="text/css">
.box {
width: 300px;
height: 700px;
border: 3px dotted gray;
}
</style>
</head>
<body>
<h3>๋ง์ฐ์ค + ctrl ๋๋ shift </h3>
<div class="box"></div>
<script type="text/javascript">
const boxEL = document.querySelector('div.box');
boxEL.addEventListener('click',e=>{
// e.ctrlkey, e.shiftKey, e.alrKey
if(e.ctrlkey){
alert('ctrl+click');
}else if(e.shiftKey){
alert('shift+click')
}
});
</script>
</body>
</html>
</head>
<body>
<h3>change event</h3>
<!--
-select : ๋๋กญ๋ค์ด์์ ๊ฐ์ ์ ํํ ๊ฒฝ์ฐ ๋ฐ์
-<textarea>, <input type="text"> : ๊ฐ์ ํธ์งํ ํฌ์ปค์ค๋ฅผ ์ฝ์๋ ๋ฐ์
-<input type="checkbox">,<input type="radio"> :๊ฐ์ ์ ํํ ๊ฒฝ์ฐ
-->
<form name="myForm">
<p>
<select name="selectEmail" onchange="changeEmail();">
<option value="">์ ํ</option>
<option value="naver.com">๋ค์ด๋ฒ</option>
<option value="hanmail.net">ํ๋ฉ์ผ</option>
<option value="gmail.com">์ง๋ฉ์ผ</option>
<option value="nate.com">๋ค์ดํธ</option>
<option value="direct">์ง์ ์
๋ ฅ</option>
</select>
<input type="text" name="email1">@ <input type="text"
name="email2" readonly>
</p>
</form>
<script type="text/javascript">
function changeEmail() {
const f = document.myForm;
let s = f.selectEmail.value;
if (s !== 'direct') {
f.email2.value = s;
f.email2.readOnly = true;
f.email2.focus();
} else {
f.email2.value = '';
f.email2.readOnly = false;
f.email2.focus();
}
}
</script>
</body>
</html>
<style type="text/css">
.box{
width: 300px;
height: 300px;
border: 3px dotted gray;
}
</style>
<script type="text/javascript">
function init() {
const boxEL = document.querySelector('.box');
boxEL.addEventListener('mouseover', e=>boxEL.style.backgroundColor='#ff0');
boxEL.addEventListener('mouseout', e=>boxEL.style.backgroundColor='');
}
//init(); ์๋ฌ : DOM์ด ์ค๋น๊ฐ ์๋ ์ํ
window.addEventListener('load',e=>init());
</script>
</head>
<body>
<h3>load ์ด๋ฒคํธ</h3>
<!--
-load ์ด๋ฒคํธ
: ์คํ์ผ๋ฑ ๋ชจ๋ ๋ฆฌ์์ค๋ฅผ ํฌํจํ์ฌ ์ ์ฒด ํ์ด์ง๊ฐ ๋ก๋๋๋ฉด ์ด๋ฒคํธ๊ฐ ์์
: HTML์ DOM ํธ๋ฆฌ ์์ฑ์ด ์๋ฃ ๋ฐ ์ด๋ฏธ์ง, ์คํ์ผ๋ฑ ์ธ๋ถ์์์ด ๋ถ๋ฌ์ค๋๊ฒ์ด ๋๋ฌ์๋ ๋ฐ์
-->
<div class="box"></div>
</body>
</html>