๐ป
JavaScript ์ ์ดํด - ๊ธฐ๋ณธ ๊ฐ๋ ๋ฐ ๋ฌธ๋ฒ/ ์ด๋ฒคํธ(Event) / ํจ์(Function) ๋ณธ๋ฌธ
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;
}
}
'KITRI > JAVASCRIPT' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JavaScript ์ ์ดํด - [AJAX] ํ์ค ๋ต๊ธ ๋ฌ๊ธฐ(CRUD) (0) | 2020.07.16 |
---|---|
JavaScript ์ ์ดํด - [AJAX] XHR(XMLHttpRequest) (0) | 2020.07.16 |
JavaScript ์ ์ดํด - [AJAX] JSON ๊ฐ์ฒด ๋ค๋ฃจ๊ธฐ (0) | 2020.07.15 |
JavaScript ์ ์ดํด - JS/jQuery ์๋์์ฑ ํ๋ฌ๊ทธ์ธ ์ค์น (0) | 2020.07.14 |
JavaScript ์ ์ดํด - ์ค๋ธ์ ํธ(Object) (0) | 2020.06.05 |