๐Ÿ’ป/Javascript

(50) [JavaScript] 6์ผ JavaScript ๊ฐ์ฒด - DOM

๋”ฐ๊ถˆ 2024. 4. 28. 12:34

 

 

 

๋ฌธ์„œ ๊ฐ์ฒด ๋ชจ๋ธ(Document Object Model)์ดํ•ด
์›น ํŽ˜์ด์ง€ ์ฝ˜์ฒธ์ธ ๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์™€ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
DOM์€ HTML๋ฌธ์„œ์˜ ๋ชจ๋“  ์š”์†Œ์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ •์˜ํ•œ  API๋‹ค.
DOM์€ ์ „์ฒด ํŽ˜์ด์ง€๋ฅผ ๋…ธ๋“œ ๊ณ„์ธต ๊ตฌ์กฐ๋กœ ๋ณ€ํ™˜ํ•˜๋ฉฐ, HTML ํŽ˜์ด์ง€์˜ ๊ฐ ๋ถ€๋ถ„์€ ๊ฐ๊ธฐ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฅผ
ํฌํ•จํ•˜๋Š” ๋‹ค์–‘ํ•œ ํƒ€์ž…์˜ ๋…ธ๋“œ๋กœ ํ‘œํ˜„๋œ๋‹ค. 

 

 

 

</head>
<body>

	<h3>document ๊ฐ์ฒด</h3>

	<form name="myForm">
		<p>
			์ด๋ฆ„ : <input type="text" name="name" >
		</p>
		<p>
			์ƒ์ผ : <input type="text" name="birth">
		</p>
		<p>
			๋‚˜์ด : <input type="text" name="age" id="age">
		</p>
		<p>
			<button type="button" onclick="result()">ํ™•์ธ</button>
		</p>
	</form>
	<hr>
	<div class="Layout"></div>

	<script type="text/javascript">
		function result() {
			//ํผ ์ ‘๊ทผ 1. ๋ฐฐ์—ด ์ ‘๊ทผ 2. ํผ ์ด๋ฆ„์œผ๋กœ ์ ‘๊ทผ 
			//const f= document.forms //'s'๋Š” ๋ฐฐ์—ด ๊ฐœ๋…, ๋ชจ๋“  ํผ์„ ๊ฐ€์ ธ์˜ด
			const f = document.myForm; // ํผ ์ด๋ฆ„์œผ๋กœ ์ ‘๊ทผ (์ด๋ฆ„์€ ๋˜‘๊ฐ™์€ ๊ฒƒ์ด ์žˆ์œผ๋ฉด ์•ˆ๋จ)
			
			//form์„ ์ด์šฉํ•˜์—ฌ form ์•„๋ž˜์˜ ์ž…๋ ฅ 
			let name = f.name.value;
			
			//getElementsByName() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ name ์†์„ฑ์œผ๋กœ ์ ‘๊ทผ
			let birth = document.getElementsByName('birth')[0].value;
			
			//getElementsByID() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ id ์†์„ฑ์œผ๋กœ ์ ‘๊ทผ
			let age = document.getElementById('age').value;
			
			let s = `์ด๋ฆ„:${name},์ƒ๋…„์›”์ผ:${birth},๋‚˜์ด:${age}`;
			
			//์„ ํƒ์ž๋กœ ์ ‘๊ทผ
			//let divEL = document.querySelectorAll('.layout')[0];
			let divEL = document.querySelector('.layout');
			divEL.innerHTML=s;
			

		}
	</script>

</body>
</html>

 

 

<style type="text/css">
.container{
	background:#fff; width: 100% ; display : flex;
	justify-content:center; align-items:center;
}
</style>

</head>
<body>

<h3>์ „์ฒด ํ™”๋ฉด</h3>

<div class ="container">
	<button type="button" class="btnFullscreen">์ „์ฒด ํ™”๋ฉด</button>
	<button type="button" class="btnExitFullscreen">์ „์ฒดํ™”๋ฉด์ทจ์†Œ</button>
</div>

<script type="text/javascript">
const btnEnterFull=document.querySelector('.btnFullscreen');
const btnExitFull=document.querySelector('.btnExitFullscreen');

const container = document.querySelector('.container');

//๋ฒ„ํŠผ์— ๋Œ€ํ•œ click ์ด๋ฒคํŠธ ๋“ฑ๋ก
btnEnterFull.addEventListener('click',e=>{
	container.requestFullscreen();
});

btnEnterFull.addEventListener('click',e=>{
	document.exitFullscreen();
});





</script>

</body>
</html>

 

 

</head>
<body>

	<h3>location ๊ฐ์ฒด</h3>
	
	<!-- 
		ํ˜„์žฌ ๋ธŒ๋ผ์šฐ์ €์— ํ‘œ์‹œ๋œ HTML ๋ฌธ์„œ์˜ ์ฃผ์†Œ๋ฅผ ์—ด๊ฑฐ๋‚˜ 
		๋ธŒ๋ผ์šฐ์ €์— ์ƒˆ๋ฌธ์„œ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ๋•Œ ์‚ฌ์šฉ
	 -->
	 
	 
	 <p> <button type ="button" onclick="fun();">๋„ค์ด๋ฒ„</button></p>
	 <p> <button type ="button" onclick="fun2();">๊ตฌ๊ธ€</button></p>
	 <p> <button type ="button" onclick="location.href('https://www.daum.net/')">๋‹ค์Œ</button> </p>
	 
	 <script type="text/javascript">
	 function fun() {
		 //์ด์ „ ํŽ˜์ด์ง€๋กœ ๋Œ์•„๊ฐˆ์ˆ˜ ์žˆ๋‹ค. 
		location.href='https://naver.com';
	}
	 
	 function fun2() {
		 //์ด์ „ํŽ˜์ด์ง€๋กœ ๋Œ์•„๊ฐˆ์ˆ˜์—†๋‹ค. ์ƒˆ๋กœ์šด ํŽ˜์ด์ง€๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ์ „์— ํžˆ์Šคํ† ๋ฆฌ ์ œ๊ฑฐ 
		location.replace('https://google.com');
	}
	
	 
	 </script>

</body>
</html>

 

</head>
<body>

	<h3>navigator</h3>
	<!-- ๋ธŒ๋ผ์šฐ์ € ๊ณต๊ธ‰์ž ๋ฐ ๋ฒ„์ „ ์ •๋ณด๋“ฑ์„ ํฌํ•จํ•œ ๋ธŒ๋ผ์šฐ์ €์— ๋Œ€ํ•œ ๋‹ค์–‘ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฐ์ฒด  -->
	
	<script type="text/javascript">
	let a = navigator.userAgent;
	let b = navigator.language;
	
	console.log('userAgent:',a);
	console.log('language:',b);
    
	</script>
    
</body>
</html>

 

</head>
<body>

	<div>
		<p><a href="javascript:winOpen();">์—ด๊ธฐ</a></p>
		<p><a href="javascript:winClose();">๋‹ซ๊ธฐ</a></p>
	</div>
	
	<script type="text/javascript">
	
	var win = null;
	
	function winOpen() {
		let flag;
		let url='https://www.naver.com';
		
		flag='left=10, top=10, width=700,height=500';
		
		win = window.open(url,'test',flag);
	}
	
	function winClose() {
		if(win){
			win.close();
		}
		 win=null;
	}
	</script>
	
</body>
</html>

 

 

 

</head>
<body>


<div>

	<form name="myForm">
	<p><input type="text" name="name" placeholder="์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”"></p>
	<p>
		<input type="text" name="zip" readonly placeholder="์šฐํŽธ๋ฒˆํ˜ธ">
		<button type ="button" onclick="zipOpen();">์šฐํŽธ๋ฒˆํ˜ธ</button>
		<input type="text" name="addr1" style="width:250px;" readonly placeholder="๊ธฐ๋ณธ์ฃผ์†Œ"><br>
		<input type="text" name="addr2" style="width:250px;" placeholder="์ƒ์„ธ์ฃผ์†Œ">
	</p> 
	<p>
	<button type="button">ํ™•์ธ</button>
	</p>
	</form>
</div>

<script type="text/javascript">
function zipOpen() {
	let flag;
	let url = 'zipFind.html';
	
	flag='left=30, top=30,width=700, height=500';
	window.open(url,'zip',flag);
	
}


</script>

</body>
</html>

 

 

</head>
<body>

	<div class="Layout"
		style="width: 300px; height: 300px; border: 3px dotted blue; margin: 30px auto;"></div>
	<p style="text-align: center;">
		<button type="button" onclick="result()">๋ฐฐ๊ฒฝ์ƒ‰ ๋ณ€๊ฒฝ</button>
	</p>

	<script type="text/javascript">
		function result() {
			let url = 'colorWin.html';
			let flag = 'top=30,left=30, width=250, height=100';
			window.open(url, 'test', flag);
		}
	</script>


</body>
</html>