day12

์ƒ์€๐Ÿ‘ธยท2023๋…„ 11์›” 20์ผ
0

๋šœ๋ฒ…๋šœ๋ฒ… ๋‘๋ฒˆ์งธ

๋ชฉ๋ก ๋ณด๊ธฐ
2/13
post-thumbnail
post-custom-banner

๐Ÿ“– html + js

1. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋…ธ๋“œ ์„ ํƒํ•˜๊ธฐ

2. ๋…ธ๋“œ / ์†์„ฑ ์กฐ์ž‘ํ•˜๊ธฐ

(1) ์ปจํ…์ธ  ์กฐ์ž‘ํ•˜๊ธฐ (๋‚ด๋ถ€ ๋‚ด์šฉ)

textContent : ๋ชจ๋“  ํ…์ŠคํŠธ
innerText : ํ™”๋ฉด์— ๋ณด์ด๋Š” ํ…์ŠคํŠธ
innetHTML : ๋ชจ๋“  ํ…์ŠคํŠธ (htmlํฌํ•จ)

(2) ์Šคํƒ€์ผ ์กฐ์ž‘ํ•˜๊ธฐ

์š”์†Œ.style.CSS์†์„ฑ = ๊ฐ’;

(3) ํด๋ž˜์Šค ์กฐ์ž‘ํ•˜๊ธฐโญ

์š”์†Œ.classList.add('class์ด๋ฆ„'); : ์ถ”๊ฐ€ -> ์›๋ž˜ ์žˆ๋˜ ์†์„ฑ + ์ถ”๊ฐ€ํ•˜๋Š” ์†์„ฑ
์š”์†Œ.classList.remove('class์ด๋ฆ„'); : ์‚ญ์ œ
์š”์†Œ.classList.toggle('class์ด๋ฆ„'); : On/Off

(4) ์†์„ฑ ์กฐ์ž‘ํ•˜๊ธฐ

getAttribute('์†์„ฑ') => ์†์„ฑ๊ฐ’ ๊ฐ€์ง€๊ณ  ์˜ค๋Š”
setAttribute('์†์„ฑ', '์†์„ฑ๊ฐ’') => ์†์„ฑ ์„ธํŒ… -> ์›๋ž˜ ์žˆ๋˜ ์†์„ฑ ๋‹ค ์—†์• ๊ณ  ์„ธํŒ…ํ•œ ์†์„ฑ๋งŒ ๋“ค์–ด๊ฐ„๋‹ค!
getAttribute('์†์„ฑ') => ์†์„ฑ์„ ์‚ญ์ œํ•˜๋Š”

(5) ๋…ธ๋“œ ์ถ”๊ฐ€/์ƒ์„ฑ/์‚ญ์ œ

๋…ธ๋“œ์ƒ์„ฑ -> ์ž์‹๋…ธ๋“œ๋กœ ์ถ”๊ฐ€!

๐Ÿ–<๋…ธ๋“œ ์ƒ์„ฑ>
์š”์†Œ ๋…ธ๋“œ : createElement => htmlํƒœ๊ทธ
ํ…์ŠคํŠธ ๋…ธ๋“œ : createTextNode => ๋‚ด๋ถ€๋‚ด์šฉ(ํ…์ŠคํŠธ๋ฌธ์ž)
์†์„ฑ ๋…ธ๋“œ : createAttribute => ํƒœ๊ทธ์˜ ์†์„ฑ
์†์„ฑ๊ฐ’์ถ”๊ฐ€
์†์„ฑ๋…ธ๋“œ.value = "๊ฐ’";

๐Ÿ–<๋…ธ๋“œ ์—ฐ๊ฒฐ>
๋…ธ๋“œ.appendChild() -> ์ž์‹ ์ถ”๊ฐ€
๋…ธ๋“œ.setAttributeNode() -> ์†์„ฑ ์—ฐ๊ฒฐ

๐Ÿ–<๋…ธ๋“œ ์‚ญ์ œ>
๋ถ€๋ชจ๋…ธ๋“œ.removeChild(์ž์‹๋…ธ๋“œ)

<div id="appendChild">
        <p>๋‚˜ ๊ธฐ๋ณธ ํ…์ŠคํŠธ</p>
</div>

div ์•ˆ์— 
<a href="http://www.naver.com" target="_blank">๋„ค์ด๋ฒ„๋กœ ์ด๋™</a>
์„ ๋„ฃ์œผ๋ ค๊ณ  ํ•˜๋Š” ์ƒํ™ฉ!
<script>
	const aNode = document.createElement('a');
  	//ํƒœ๊ทธ ์ƒ์„ฑ <a></a>
	const hrefAttrNode = document.createAttribute('href');
	hrefAttrNode.value = "http://www.naver.com";
	//์†์„ฑ ์ƒ์„ฑ href="http://www.naver.com";
	const targetAttrNode = document.createAttribute('target');
	targetAttrNode.value = "_blank";
	//์†์„ฑ ์ƒ์„ฑ target="_blank";
	const aTextNode = document.createTextNode("๋„ค์ด๋ฒ„๋กœ ์ด๋™");
	//๋‚ด๋ถ€๋‚ด์šฉ ์ƒ์„ฑ ๋„ค์ด๋ฒ„๋กœ ์ด๋™

	//๋‹ค ๊ฐ์ž ๋”ฐ๋กœ ๋งŒ๋“ค์–ด ์ง„ ์ƒํƒœ๋ผ ๊ฐ๊ฐ ์—ฐ๊ฒฐ ์‹œ์ผœ์ค˜์•ผํ•œ๋‹ค! ๋ถ€๋ชจ๋…ธ๋“œ.(์ž์‹๋…ธ๋“œ)
	aNode.setAttrributeNode(hrefAttrNode);
	aNode.setAttrributeNode(targetAttrNode);
	aNode.appendChild(aTextNode);
	//<a href="http://www.naver.com" target="_blank">๋„ค์ด๋ฒ„๋กœ ์ด๋™</a>

	//div ํƒœ๊ทธ์— aNode ์—ฐ๊ฒฐ
	const div_ac = document.getElementById('appendChild');
	div_ac.appendChild(aNode);
	// <div id="appendChild"> <a href="http://www.naver.com" target="_blank">๋„ค์ด๋ฒ„๋กœ ์ด๋™</a> </div>


	//๋…ธ๋“œ ์‚ญ์ œ
	const pRemove = document.querySelector('#appendChild > p');
	div_ac.removeChild(pRemove);
	//<p>๋‚˜ ๊ธฐ๋ณธ ํ…์ŠคํŠธ</p> ์‚ญ์ œ!
</script>

3. ์ด๋ฒคํŠธ ์—ฐ๊ฒฐํ•˜๊ธฐ

๋ˆ„๊ฐ€, ์–ธ์ œ, ๋ฌด์—‡์„ ์‹คํ–‰

์–ด๋Š ํƒœ๊ทธ ์š”์†Œ๊ฐ€ -> ์–ด๋–ค ์ด๋ฒคํŠธ ๋ฐœ์ƒ -> ๋™์ž‘ ์ˆ˜ํ–‰
(ํƒœ๊ทธ ์„ ํƒ) (์ด๋ฒคํŠธ ๋“ฑ๋ก) (์ˆ˜ํ–‰ js ์ฝ”๋“œ - ํ•จ์ˆ˜)

โœŒ 1. ๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ
ํด๋ฆญ : onclick @
๋”๋ธ”ํด๋ฆญ : ondbclick
๋งˆ์šฐ์Šค์˜ค๋ฒ„ : onmouseover
๋งˆ์šฐ์Šค๊ตฌ์—ญ๋น ์ ธ๋‚˜๊ฐˆ๋•Œ : onmouseout
๋งˆ์šฐ์Šค ์›€์ง์ผ๋•Œ : onmousemove
ํœ  ์›€์ง์ผ๋•Œ : onwheel

โœŒ 2. ํ‚ค๋ณด๋“œ ์ด๋ฒคํŠธ
onkeypress : ๋ˆ„๋ฅด๊ณ  ์žˆ์„๋•Œ
onkeydown : ๋ˆ„๋ฅผ๋•Œ @
onkeyup : ๋ˆŒ๋ €๋‹ค๊ฐ€ ๋—„ ๋•Œ @

โœŒ 3. ํฌ์ปค์Šค
onfocus : ํฌ์ปค์Šค๊ฐ€ ๋์„๋•Œ(๋ˆŒ๋Ÿฌ์„œ ํ™œ์„ฑํ™”)
onblur : ํฌ์ปค์Šค ์‚ฌ๋ผ์งˆ๋•Œ

๐Ÿคฒ์ด๋ฒคํŠธ ๋“ฑ๋ก
1) ์ธ๋ผ์ธ ๋“ฑ๋ก
ํƒœ๊ทธ ๋‚ด๋ถ€์— onclick=ํ•จ์ˆ˜()
ํƒœ๊ทธ ๋‚ด๋ถ€์— onmouseover=ํ•จ์ˆ˜()
2) ํ”„๋กœํผํ‹ฐ ๋“ฑ๋ก => ๋งจ๋งˆ์ง€๋ง‰์— ์ž‘์„ฑํ•œ ํ•จ์ˆ˜ 1๊ฐœ๋งŒ ๋ฐœ์ƒ (๋ฎ์–ด์“ฐ๊ธฐ) : setAttribute('class', )
ํƒœ๊ทธ์š”์†Œ.onclick = ํ•จ์ˆ˜()
3) ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ์ถ”๊ฐ€ => ํ•จ์ˆ˜ 2๊ฐœ ๋‹ค ๋ฐœ์ƒ (์ถ”๊ฐ€๋˜๋Š” ๊ฐœ๋…) : classList.add
ํƒœ๊ทธ์š”์†Œ.addEventListener('์ด๋ฒคํŠธ ์ข…๋ฅ˜', '์‹คํ–‰ํ• ํ•จ์ˆ˜๊ธฐ๋Šฅ'); !!!<on ์ œ์™ธ>!!!

๐Ÿ‘Š๊ธฐ๋ณธ์ด๋ฒคํŠธ ์ทจ์†Œํ•˜๊ธฐ
e.preventDefault();

๐Ÿ‘Šform - submit ์ „์†ก
1) input type="submit" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ๋•Œ
2) button type="submit" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ๋•Œ
3) ๊ฐ•์ œ๋กœ form.submit() ํ˜ธ์ถœํ• ๋•Œ

๐Ÿ‘Šform ํƒœ๊ทธ ์กฐ์ž‘ํ•˜๊ธฐ
1) submit() -> form submit ํ˜ธ์ถœ
2) click() -> button ํด๋ฆญ ์ด๋ฒคํŠธ ํ˜ธ์ถœ
3) focus() -> ํ•ด๋‹น ์š”์†Œ๋กœ ํฌ์ปค์Šค ์ด๋™

<button id="btn_test" onclick="btnClickEvent()">๋ˆŒ๋Ÿฌ๋ด</button>
//์ธ๋ผ์ธ ๋“ฑ๋ก

<button id="btn_test2">๋ˆŒ๋Ÿฌ๋ด222</button>

<button id="btn_test3">๋ˆŒ๋Ÿฌ๋ด333</button>
<script>
  	function btnClickEvent() {
		console.log('๋ˆŒ๋Ÿฌ๋ด ๋ฒ„ํŠผ ๋ˆŒ๋ฆผ');
	}	
  
	//ํ”„๋กœํผํ‹ฐ ๋“ฑ๋ก ๋ฐฉ์‹
	function btn2ClickEvent() { //ํ•จ์ˆ˜
    	alert('๋ฒ„ํŠผ2 ๋ˆŒ๋ฆผ'); 
    }
	const btn2Event = () => { //ํ•จ์ˆ˜
      	alert('๋ฒ„ํŠผ2 event2');
    }
    
    const btnTest2 = document.querySelector('#btn_test2');

	btnTest2.onclick = ()=> {
     		 					alert('๋ฒ„ํŠผ2 event2');
    						} //์ง์ ‘ ์ต๋ช…ํ•จ์ˆ˜ ์ •์˜(1ํšŒ์„ฑ ํ•จ์ˆ˜)
    
    //ํ”„๋กœํผํ‹ฐ ๋“ฑ๋ก ๋ฐฉ์‹
  	btnTest2.onclick = function() {
      	alert('๋ฒ„ํŠผ2 ๋‚ด๋ถ€ function() ์ต๋ช… ๋“ฑ๋ก'); 
    }

	//์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ๋ฐฉ์‹
	const btn3Func = () => { //ํ•จ์ˆ˜
      alert('btn3๊ฐ€ ๋ˆŒ๋ฆผ ์ด๋ฒคํŠธ๋ฆฌ์Šค๋„ˆ ์—ฐ๊ฒฐ');
    }
    
    const btnTest3 = document.getElementById(btn_test3);
	
	btnTest3.addEventListener('click', btn3Func);
	btnTest3.addEventListener('click', ()=>{
    	alert('์ด๋ฒคํŠธ๋ฆฌ์Šค๋„ˆ ๋‚ด๋ถ€ํ•จ์ˆ˜ ๋ฐ”๋กœ ์„ ์–ธ'); 
    });
	//์ค‘์š”ํ•œ๊ฑด onclick์ด๋ผ๊ณ  ์“ฐ๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ on์„ ๋นผ๊ณ  click๋งŒ ์ž‘์„ฑํ•œ๋‹ค!
    
</script>

=> ๐Ÿ’ป ์ธ๋ผ์ธ ๋“ฑ๋ก

=> ๋ˆŒ๋Ÿฌ๋ด ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด

=> ์ฝ˜์†”์ฐฝ์— ์ด๋ ‡๊ฒŒ ๋œธ

=> ๐Ÿ’ป ํ”„๋กœํผํ‹ฐ ๋“ฑ๋ก

=> ๋ˆŒ๋Ÿฌ๋ด222 ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด

=> ์ง์ ‘ ์ต๋ช…ํ•จ์ˆ˜ ์ •์˜ ์ ์šฉํ•ด๋…ผ ํ•จ์ˆ˜๋Š” xxx
=> ๋ฎ์–ด์“ฐ๊ธฐ ๋˜๊ธฐ๋•Œ๋ฌธ์— ๋งจ๋‚˜์ค‘์— ์ ์šฉ์‹œ์ผœ๋…ผ ํ•จ์ˆ˜๋งŒ ์ ์šฉ๋œ๋‹ค!

=> ๐Ÿ’ป ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ๋“ฑ๋ก

=> ๋ˆŒ๋Ÿฌ๋ด333 ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด

=> classList.add์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•จ์ˆ˜ 2๊ฐœ๊ฐ€ ๋‹ค ๋ฐœ์ƒํ•œ๋‹ค!

<div id="tg2">
	<input type="text" id="tg1">
</div>
<script>
	const input_tg1 = document.getElementById('tg1');
	
  	input_tg1.addEventListener('keydown', (e)=>{ //keydown์ด ๋ฐœ์ƒํ•˜๋ฉด ๋งค๊ฐœ๋ณ€์ˆ˜e ํ™œ์„ฑํ™”
       	console.log(e.keyCode); //์•„์Šคํ‚ค์ฝ”๋“œ
        console.log(e.shiftKey);
        console.log(e.altKey);
        console.log(e.ctrlKey);
    });
  
</script>

=> ๐Ÿ’ป


=> abc ์ž…๋ ฅํ–ˆ์„๋•Œ ์ž๋™์œผ๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜ e ํ™œ์„ฑํ™”
=> shift, ctrl, alt ๋‹ค ์ž…๋ ฅ์•ˆํ–ˆ์œผ๋‹ˆ๊นŒ false๋กœ ๋œจ๋Š”๊ฒƒ!

<form id="form_tg">
	<p>์•„์ด๋”” <input type="text" id="input_id"></p>
    <p>๋น„๋ฐ€๋ฒˆํ˜ธ <input type="password" id="input_pw"></p>
    <button type="submit" id="btn_submit">submit๋ฒ„ํŠผ</button>
	<!--submit์€ buttonํ•œํ…Œ ๊ฑธ์ˆ˜x formํ•œํ…Œ ๊ฑฐ๋Š”๊ฑฐ์ง€(ํŽ˜์ด์ง€์ƒˆ๋กœ๊ณ ์นจ)-->
    <button type="button" id="btn_normal">๊ทธ๋ƒฅ๋ฒ„ํŠผ</button>
</form>
<script>
	const form_tg = document.getElementById('form_tg'); //ํผํƒœ๊ทธ ์„ ํƒ
</script>
profile
๋’ค์ฃฝ๋ฐ•์ฃฝ ๋ฒจ๋กœ๊ทธ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€