๐Ÿ’ป

JavaScript ์˜ ์ดํ•ด - ๊ธฐ๋ณธ ๊ฐœ๋… ๋ฐ ๋ฌธ๋ฒ•/ ์ด๋ฒคํŠธ(Event) / ํ•จ์ˆ˜(Function) ๋ณธ๋ฌธ

KITRI/JAVASCRIPT

JavaScript ์˜ ์ดํ•ด - ๊ธฐ๋ณธ ๊ฐœ๋… ๋ฐ ๋ฌธ๋ฒ•/ ์ด๋ฒคํŠธ(Event) / ํ•จ์ˆ˜(Function)

๋˜ํšจ๋‹ˆ 2020. 6. 4. 11:09
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<script type="text/javascript">
	if(true){
		document.write("<h3>๊ฐ€์ž…๊ฐ€๋Šฅํ•œ ์•„์ด๋”” ์ž…๋‹ˆ๋‹ค.</h3>");
	}
	</script>
</body>
</html>

JavaScript

 

JavaScript๋Š” ์›นํŽ˜์ด์ง€๋ฅผ ๋™์ ์œผ๋กœ, ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ ์œผ๋กœ ์ œ์–ดํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ณ ์•ˆ๋œ ์–ธ์–ด๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋Š˜๋‚  ๊ฐ€์žฅ ์ค‘์š”ํ•œ ํ”Œ๋žซํผ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋Š” ์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ์œ ์ผํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ด๋‹ค. ์ตœ๊ทผ์—๋Š” HTML5์˜ ์ ์šฉ์ด ๊ฐ€์†ํ™”๋˜๋ฉด์„œ ์ง€๊ธˆ๊นŒ์ง€ ๋ชจ๋ฐ”์ผ ํ™˜๊ฒฝ์—์„œ ๋„ค์ดํ‹ฐ๋ธŒ ์•ฑ(์•ˆ๋“œ๋กœ์ด๋“œ, IOS)์œผ๋กœ ๊ตฌํ˜„ํ•ด์™”๋˜ ๊ธฐ๋Šฅ์ด ์›น์—์„œ๋„ ๋Œ€๋ถ€๋ถ„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜๊ณ  ์žˆ๋‹ค. ์›น์ด ํฌ๋กœ์Šคํ”Œ๋žซํผ์ด๋ผ๋Š” ์ , ๊ฒ€์ƒ‰ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์ , ๋„ค์ดํ‹ฐ๋ธŒ ๋””๋ฐ”์ด์Šค๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋Š” ํ•˜๋“œ๋ธŒ๋ฆฌ๋“œ ์‹œ์Šคํ…œ(phonegap ๋“ฑ)์ด ์กด์žฌํ•œ๋‹ค๋Š” ์ ์—์„œ ์›น์˜ ์ค‘์š”ํ•จ์€ ๋”์šฑ ํ™•๋Œ€๋  ์ „๋ง์ด๋‹ค. ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์›น์—์„œ ๊ตฌ๋™๋˜๋Š” ์œ ์ผํ•œ ์–ธ์–ด์ธ JavaScript์˜ ์ค‘์š”ํ•จ๋„ ์ ์  ์ปค์งˆ ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋œ๋‹ค.

 

1. HTML : ์›นํŽ˜์ด์ง€๋ฅผ ์ œ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ ๋„๊ตฌ
2. CSS  : HTML ํ‘œํ˜„(๊พธ๋ฉฐ์ค€๋‹ค) ํ•œ๋‹ค.
3. JavaScript
         1) HTML ํ†ต์ œํ•œ๋‹ค.
         2) ๊ตฌ์„ฑ
                  - Core : ๊ธฐ๋ณธ๋ฌธ๋ฒ• ๊ตฌ์กฐ(๋ฐ์ดํ„ฐ ํƒ€์ž…, ๋ฐ˜๋ณต๋ฌธ, ํ•จ์ˆ˜ ๋“ฑ๋“ฑ)
                  - BOM(Browser Object Model) : ๋ธŒ๋ผ์šฐ์ € ๊ด€๋ จ๋œ ๊ฐ์ฒด 
                  - DOM(Document Object Model): Element ์ œ์–ด, Style ์ œ์–ด, Attribute ์ œ์–ด, Event ์ œ์–ด


 

JavaScript HTML DOM Document

 

 

 

 

01_Core.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	// ์ž๋ฃŒํ˜• (์ˆซ์ž, ๋ฌธ์ž(๋ฌธ์ž์—ด), ๋ฐฐ์—ด, ํด๋ž˜์Šค) : ๋ฐ์ดํ„ฐ ์ž๋ฃŒํ˜•์— ์—„๊ฒฉํ•˜์ง€ ์•Š๋‹ค.
	var su = 10;
	var value = 23.5;
	var hap = su + value;
	var str = "์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ";
	
	document.write("<p style='color:blue;'>" + hap + "</p>");
	document.write("<p>" + str + "</p>");
	
	// ์กฐ๊ฑด๋ฌธ (if, if-else, if-elsif, switch)
	if(su > 20){
		document.write("<h3> 20๋ณด๋‹ค ํฐ ์ˆ˜์ž…๋‹ˆ๋‹ค. </h3>");
	}else if(su > 30){
		document.write("<h3> 30๋ณด๋‹ค ํฐ ์ˆ˜์ž…๋‹ˆ๋‹ค. </h3>");
	}else{
		document.write("<h3> ๊ธฐํƒ€๋“ฑ๋“ฑ </h3>");	
		// document ๋‚ด์žฅ๊ฐ์ฒด : new๋ผ๋Š” ํ‚ค์›Œ๋“œ๋กœ ๊ฐ์ฒด๋ฐœ์ƒ ์•ˆํ•˜๊ณ  ์‚ฌ์šฉํ•œ๋‹ค.
		// ๋‚ด์žฅํ•จ์ˆ˜ alert() : ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ธŒ๋ผ์šฐ์ € ๊ฐ์ฒด์ค‘์— window ๊ฐ์ฒด์ธ๋ฐ ๊ฐ๋ช… ์กฐ์ฐจ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
	}
	// ๋ฐ˜๋ณต๋ฌธ (for๋ฌธ, while๋ฌธ, do-while๋ฌธ)
	for(var i = 0; i < 10; i++){
		document.write("<div> ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. " + i + "</div>");
	}
</script>
</head>
<body>
</body>
</html>

 

 

 

02_Core.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	var suArr = [1,2,3,4,5];
	var strArr = new Array("apple", "banana", "melon");
	document.write("<h3>" + suArr.length + "</h3>");
	document.write("<h3>" + strArr.length + "</h3>");
	
	for(var i=0; i<suArr.length; i++){
		document.write("<h3>" + suArr[i] + "</h3>");
	}
	for(var i=0; i<suArr.length; i++){
		document.write("<h3>" + strArr[i] + "</h3>");
	}
	
	var array=[];	//new Array()
	array[0]=25.7;
	array[1]=15;
	array[2]="hi";
	for(var i = 0; i<strArr.length; i++){
		document.write("<h3>" + array[i] + "</h3>");
	}
</script>
</head>
<body>
</body>
</html>

 


์ด๋ฒคํŠธ(์‚ฌ์šฉ์ž ์•ก์…˜)๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ๋‚ด๋ถ€์ ์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ์ฐพ์•„๊ฐ€๋Š” ๊ฒƒ ->  handler

์ฝœ๋ฐฑํ•จ์ˆ˜๋Š” ์‹œ์Šคํ…œ์ด ์ฐพ์•„๊ฐ€๋Š” ๊ฒƒ

๋‚˜๋จธ์ง€ ํ•จ์ˆ˜๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ํ˜ธ์ถœํ•ด์ฃผ๋Š” ๊ฒƒ

 

 

 

03_Function.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	function sub(){	
		document.write("<h3> sub function ์ž…๋‹ˆ๋‹ค.</h3>");
	}
	
	function hap(a, b){
		document.write("<h3>"+ a + "," + b + "</h3>");
	}
	
	function apple(){
		return "apple";	
	}
	
	function banana(a, b){
		return a-b;
	}
	
	sub();
	hap(10, 20);
	
	var str = apple();
	document.write("<h3>"+ str +"</h3>");
	
	var su = banana(20, 30);
	document.write("<h3>"+ su +"</h3>");
	
</script>
</head>
<body>

</body>
</html>

 

 

 

04_MouseEvent.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	function myClick(){
		window.alert("myClick Function"); //BOM ๊ฐ์ฒด ์ค‘ window ๋‚ด์žฅ ๊ฐ์ฒด(window๊ฐ์ฒด๋Š” ์ƒ๋žต ๊ฐ€๋Šฅ)
	}
	
	function mySub(){
		window.alert("mySub Function"); 
	}
	
	function dblSub(){
		window.alert("dblSub Function"); 
	}
	
	function bigText(obj){
		obj.style.color = "red";
		obj.style.fontSize = "40px";
		obj.style.fontFamily = "๊ถ์„œ์ฒด";
		obj.href="https://www.naver.com";
	}
	
	function normalText(obj){
		obj.style.color = "blue";
		obj.style.fontSize = "80px";
		obj.style.fontFamily = "๋ง‘์€๊ณ ๋”•";
	}
</script>
</head>
<body>
	<!-- ๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ: onclick(mousedown, mouseup), dblclick,
				    css/jquery hover(mouseover, mouseout) -->
	
	<input type="button" value="ํ™•์ธํ•˜์„ธ์š”." onclick="myClick()"/>
	<br/><br/>
	
	<span onclick="myClick()" style="cursor:ponter;">๋ฐฐ๊ณ ํŒŒ~~~~~</span>
	<br/><br/>
	
	<input type="button" value="ํ™•์ธํ•˜์„ธ์š”" onmousedown="myClick()"/>
	<input type="button" value="ํ™•์ธํ•˜์„ธ์š”" onmouseup="mySub()"/>
	<input type="button" value="ํ™•์ธํ•˜์„ธ์š”" ondblclick="dblSub()"/>
	<br/><br/>
	
	<a href="#" style="text-decoration: none;" onmouseover="bigText(this)" onmouseout="normalText(this)">
		์•ˆ๋…•ํ•˜์„ธ์š”.
	</a>
</body>
</html>

์œˆ๋„์šฐ(window) ๋‚ด์žฅ ๊ฐ์ฒด๋Š” ์ƒ๋žต์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

 

 

 

 

05_MouseEvent.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	function myFunc(){
		window.open("http://localhost:8181/webTesting/JavaScript/06_idCheck.html", "", "width=300px; height=200px");
		//"์ฃผ์†Œ","์œˆ๋„์šฐ์ด๋ฆ„","๊ฐ€๋กœ์„ธ๋กœ์Šคํฌ๋กค"
	}
	
	function myNaver(){
		window.open("https://www.naver.com", "", "width=300px, height=200px, scrollbars=yes");
	}
</script>
</head>
<body onload="myNaver()">
	<span>์•„์ด๋””:</span>
	<input type="text" name="id"/>
	<input type="button" value="์•„์ด๋”” ํ™•์ธ" onclick="myFunc()"/>
</body>
</html>

 

06_idCheck.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<script type="text/javascript">
	if(true){
		document.write("<h3>๊ฐ€์ž…๊ฐ€๋Šฅํ•œ ์•„์ด๋”” ์ž…๋‹ˆ๋‹ค.</h3>");
	}
	</script>
</body>
</html>

 

 

 

 

 

 

07_keyEvent.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	function myFun1(obj){
		obj.style.color="red";
	}
	function myFun2(){
		alert("keyUp");
	}
	function myFun3(){
		alert("keyDown");
	}
</script>
</head>
<body>
	<!-- ํ‚ค ์ด๋ฒคํŠธ: keypress(keydown, keyup) -->
	
	<input type="text" onkeypress="myFun1(this)"/>
	<br/><br/>
	
	<input type="text" onkeyup="myFun2()"/>
	<br/><br/>
	
	<input type="text" onkeydown="myFun3()"/>
	<br/><br/>
</body>
</html>

 

 

 

08_FormEvent.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	function myFocus(obj){
		obj.value="์•„์ด๋””๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.";
	}
	function myBlur(obj){
		obj.value="์ค‘๋ณต๋˜๋Š” ์•„์ด๋””๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.";
	}
	function myChange(obj){
		alert(obj.value);
	}
	function mySelect(obj){
		alert();
	}
</script>
</head>
<body>
	<!-- ํผ ์ด๋ฒคํŠธ:focus, blur, change, select, submit -->
	
	<input type="text" onfocus="myFocus(this)" onblur="myBlur(this)"/>
	<br/><br/>
	
	<select onchange="myChange(this)">
		<option value="์‚ฌ๊ณผ">์‚ฌ๊ณผ</option>
		<option value="๋ฐ”๋‚˜๋‚˜">๋ฐ”๋‚˜๋‚˜</option>
		<option value="๋”ธ๊ธฐ">๋”ธ๊ธฐ</option>
		<option value="๋ฉ”๋ก ">๋ฉ”๋ก </option>
		<option value="๋ฐฐ">๋ฐฐ</option>
	</select>
	<br/><br/>
	
	<input type="text" value="์•ˆ๋…•ํ•˜์„ธ์š”. ์ง‘์— ๊ฐ€์ž!!!!!" onselect="mySelect(this)"/>
</body>
</html>

 

 

 

 

09_FormEvent.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	function checkForm(obj){
		if(obj.irum.value==""){
			alert("์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”.");
			return false;
		}
	}
</script>
</head>
<body>
	<form action="https://www.naver.com" method="get" onsubmit="return checkForm(this)">
		<label>์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”.</label>
		<input type="text" name="irum" />
		<br/><br/>
		
		<input type="submit" value="์ „์†ก"/>
		<input type="reset" value="์ทจ์†Œ"/>
	</form>
</body>
</html>

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ๋”ฐ๋กœ ๋ถ„๋ฆฌํ•ด์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="09_Form.js"></script>
</head>
<body>
	<form action="https://www.naver.com" method="get" onsubmit="return checkForm(this)">
		<label>์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”.</label>
		<input type="text" name="irum" />
		<br/><br/>
		
		<input type="submit" value="์ „์†ก"/>
		<input type="reset" value="์ทจ์†Œ"/>
	</form>
</body>
</html>

 

 

 

focus() ํ•ด๋‹น ์š”์†Œ์— ํฌ์ปค์Šค๋ฅผ ๋ถ€์—ฌํ•˜์—ฌ

1. ํ…์ŠคํŠธ ์ฐฝ์˜ ๊ฒฝ์šฐ, ์ปค์„œ๋ฅผ ์œ„์น˜์‹œ์ผœ ๋ฐ”๋กœ ์ž…๋ ฅ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

2. ๋ฒ„ํŠผ์˜ ๊ฒฝ์šฐ ์—”ํ„ฐ ํ‚ค๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ ํด๋ฆญ ํšจ๊ณผ๋ฅผ ๋‚ธ๋‹ค.

 

blur() focus()์™€ ๋ฐ˜๋Œ€๋˜๋Š” ๊ธฐ๋Šฅ์œผ๋กœ

1. ์ฐฝ์˜ ๊ฒฝ์šฐ ์ตœ์†Œํ™” ์‹œ์ผœ๋ฒ„๋ฆฐ๋‹ค.

 

 

 

์œ ํšจ์„ฑ ์ฒดํฌ

09_FormEvent.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="09_Form.js"></script>
</head>
<body>
	<form action="https://www.naver.com" method="get" onsubmit="return checkForm(this)">
		<label>์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”.</label>
		<input type="text" name="irum" />
		<br/><br/>
		
		<label>์ด๋™ํ•  ์‚ฌ์ดํŠธ</label>	
		<select name="siteUrl">
			<option value="https://www.naver.com">๋„ค์ด๋ฒ„</option>
			<option value="https://www.daum.co.kr">๋‹ค์Œ</option>
			<option value="https://www.nate.com">๋„ค์ดํŠธ</option>
		</select>
		<br /><br />
		
		<label>์ข‹์•„ํ•˜๋Š” ๊ณผ์ผ์„ ์„ ํƒํ•˜์„ธ์š”</label>
		<input type="radio" name="fruit" value="๋ฐ”๋‚˜๋‚˜"/>
		<label>๋ฐ”๋‚˜๋‚˜</label>
		<input type="radio" name="fruit" value="์‚ฌ๊ณผ"/>
		<label>์‚ฌ๊ณผ</label>
		<input type="radio" name="fruit" value="ํŒŒ์ธ์• ํ”Œ"/>
		<label>ํŒŒ์ธ์• ํ”Œ</label>
		<input type="radio" name="fruit" value="๋”ธ๊ธฐ"/>
		<label>๋”ธ๊ธฐ</label>
		<br /><br />
		
		<label>๊ด€์‹ฌ์‚ฌ๋ฅผ ์„ ํƒํ•˜์„ธ์š”</label>
		<input type="radio" name="interest" value="๊ฒฝ์ œ"/>
		<label>๊ฒฝ์ œ</label>
		<input type="radio" name="interest" value="์‚ฌํšŒ"/>
		<label>์‚ฌํšŒ</label>
		<input type="radio" name="interest" value="IT"/>
		<label>IT</label>
		<input type="radio" name="interest" value="๋ฏธ์ˆ "/>
		<label>๋ฏธ์ˆ </label>
		<br /><br />
		
		
		<input type="submit" value="์ „์†ก"/>
		<input type="reset" value="์ทจ์†Œ"/>
	</form>
</body>
</html>

09_Form.js

function checkForm(obj){
	if(obj.irum.value==""){
		alert("์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”.");
		obj.irum.focus();
		return false;
	}
	if(obj.irum.value.length != 3){
		alert("์ด๋ฆ„์„ ์ •ํ™•ํžˆ ์ž…๋ ฅํ•˜์„ธ์š”.");
		obj.irum.focus();
		return false;
	}
	if(obj.siteUrl.value==""){
		alert("์ด๋™ํ•  ์‚ฌ์ดํŠธ๋ฅผ ์„ ํƒํ•˜์„ธ์š”.");
		obj.siteUrl.focus();
		return false;
	}
	 
	var rcheck = false;
	for(var i=0; i<obj.fruit.length; i++){
		if(obj.fruit[i].checked==true){
			rcheck = true;
		}
	}
	if(rcheck==false){
		alert("์ข‹์•„ํ•˜๋Š” ๊ณผ์ผ์„ ํ•˜๋‚˜ ์„ ํƒํ•˜์„ธ์š”.");
		obj.fruit[0].focus();
		return false;
	}
	
	var check = false;
	for(var i=0; i<obj.interest.length; i++){
		if(obj.interest[0].checked==true)
			check=flase;
	}
	if(check==false){
		alert("๊ด€์‹ฌ์‚ฌ๋ฅผ ํ•˜๋‚˜ ์„ ํƒํ•˜์„ธ์š”.");
		obj.interest[0].focus();
		return false;
	}
}

๋ฐ˜์‘ํ˜•
Comments