** ์๋ฆฌ๋ ์์ ๋ด์ฉ + ๊ฐ์ธ์ ์ผ๋ก ํ์ตํ ๋ถ๋ถ์ ์์ด ํฌ์คํ ์ ์์ฑํ๋ฏ๋ก,
์์ ํ๋ฆ ๋ฐ ๋ด์ฉ๊ณผ ํฌ์คํ ์ด ์์ ํ๊ฒ ์ผ์นํ์ง๋ ์์ต๋๋ค. (์ฝํ ์ธ ์ค๋ฅ ์ง์ ํ์)
ํต์ฌ ๊ฐ๋ : 'defer' ์ 'use strict'
์๋ฐ์คํฌ๋ฆฝํธ ๊ณต์์ฌ์ดํธ๋
์ดํฌ๋ง http://ecma-international.org/
Welcome to Ecma International
Welcome Ecma International is an industry association founded in 1961, dedicated to the standardization of information and communication systems. We are also here: Since 1961 and continuing in full force today, Ecma International®facilitates the timely
ecma-international.org
MDN https://developer.mozilla.org/ko/docs/Web
๊ฐ๋ฐ์๋ฅผ ์ํ ์น ๊ธฐ์
์น(World Wide Web)์ ๊ฐ๋ฐฉ์ฑ์ ์น์ฌ์ดํธ๋ ์จ๋ผ์ธ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ ์ํ๋ ค๋ ์ฌ๋๋ค์๊ฒ ๋ง์ ๊ธฐํ๋ฅผ ์ ๊ณตํฉ๋๋ค. ํ์ง๋ง ๊ทธ ์ฌ์ฉ ๋ฐฉ๋ฒ์ ์์์ผ ์น ๊ธฐ์ ์ ์ ํ์ฉํ ์ ์์ต๋๋ค. ์๋์ ๋งํฌ๏ฟฝ
developer.mozilla.org
w3school - ๊ณต์ ์ฌ์ดํธ๋ ์๋
์ธ๋ถ ์คํฌ๋ฆฝํธ ํ์ผ ์ฐ๊ฒฐ
<script src=์ด๋ฆ.js></script>
์๋ฐ์คํฌ๋ฆฝํธ ์ ๋ ฅ๊ณผ ์ถ๋ ฅ
prompt() ํจ์ - ์ฌ์ฉ์์๊ฒ ์ ๋ ฅ๊ฐ ๋ฐ๊ธฐ
prompt(); ํ๋กฌํํธ ์ฐฝ ๋ํ๋๋ค > ๋ด์ฉ ์ ๋ ฅ ๊ฐ๋ฅ
prompt("์ด๋ฆ์ ์ ๋ ฅํ์ธ์."); ์ ๋ ฅํ ๋ด์ฉ์ด ํจ๊ป ํ์๋๋ค
var name=prompt("์ด๋ฆ: ");
alert() ํจ์ - ์๋ฆผ ์ฐฝ์ผ๋ก ์ถ๋ ฅํ๊ธฐ
alert("ํ์ํฉ๋๋ค"); ์ ๋ ฅํ ๋ด์ฉ์ด ์๋ฆผ ์ฐฝ์ ํ์๋๋ค
document.write() ํจ์ - ์น ๋ธ๋ผ์ฐ์ ํ๋ฉด์ ์ถ๋ ฅํ๊ธฐ
๊ฒฐ๊ณผ๊ฐ์ ๋ธ๋ผ์ฐ์ ์ ์ถ๋ ฅํ๋ ์ฉ๋์ด๋ค
var name=prompt("์ด๋ฆ: ");
document.write(name + "๋, ์ด์์ค์ธ์!"); > ํ๊ธธ๋ ์ฐ๋ฉด > ํ๊ธธ๋๋, ์ด์์ค์ธ์! ๋ผ๊ณ ์ถ๋ ค๋จ
console.log() ํจ์ - ์ฝ์์ ์ถ๋ ฅํ๊ธฐ
์ฌ์ฉ ๋ฐฉ๋ฒ์ document.wirte()์ ๊ฐ๋ค
<my ์ค์ต>
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="iocheck.js"></script>
</head>
<body></body>
</html>
iocheck.js
const username = prompt("์ด๋ฆ:");
const welcomeNotice = "ํ์ํฉ๋๋ค!" + username;
alert(welcomeNotice);
document.write(username, "์ ๋ฑ์ฅ!");
๋ฐ๋ชฉ๋ฌธ
- while(true/false){~~@~~} else{~~#~~} : true๊ฐ ๋์ค๋ฉด @๊ฐ ๊ณ์ ๋ฐ๋ณต๋๋ค. false๊ฐ ๋ ๋๊น์ง.
- i=0;
while(i<10){
document.write("hello world");
i=i+1;
} :10์ด ๋๊ธฐ ์ ๊น์ง ๋ฐ๋ณตํ๋ค.
- <ul>
<script>
i=0;
while(i<10){
document.write("<li>hello world</li>");
i=i+1;
}
</script>
</ul> : ๋ฆฌ์คํธ๋ก ํํ๋๋ค.
๋ฐฐ์ด
- list=new Array("one", "two", "three"); : ๋ฐฐ์ด์ ํญ๋ชฉ์ ๋์ดํ๋ค.
- document.write(list[0]); : ๋ช๋ฒ์งธ ํญ๋ชฉ์ธ์ง ์์์๋ถํฐ ์ผ๋ค. 0๋ถํฐ ์์ํ๋ค.
- document.write(list.length); : ๋ฐฐ์ด์ ๋ช๊ฐ๊ฐ ์๋์ง ์ ์ ์๋ค.
๋ฐฐ์ด๊ณผ ๋ฐ๋ณต๋ฌธ
- list=new Array("๊ฐ","๊ฐ","์ ","๋");
i=0;
while(i<list.length){
document.write(list[i]);
i=i+1; : ๊ฐ๊ฐ์ ๋ ๋ผ๊ณ ๋์จ๋ค.
} : ๋ฐฐ์ด์ ๊ฐ์์ ์๋ง๊ฒ ๋ค ๋์จ๋ค.
- <ul>
<script>
list=new Array("๊ฐ","๊ฐ","์ ","๋");
i=0;
while(i<list.length){
document.write("<li>"+list[i]+"</li>"); : ๊ผญ ํฐ๋ฐ์ดํ์ ํ๋ฌ์ค ๊ธฐํธ๋ฅผ ์จ์ค๋ค.
i=i+1;
}
</script>
</ul> : ๋ฆฌ์คํธ ํ์์ผ๋ก ๋์จ๋ค.
<my ์ค์ต>
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<ul>
<script src="list.js"></script>
</ul>
</body>
</html>
list.js
let i = 0;
while (i < 10) {
document.write("<li>๊ณง ์์ผ๋ฉด 4์์
๋๋ค. ์ ์ ๋ฐ์ง ์ฐจ๋ ค์ผํ ๊ฒ</li>");
i = i + 1;
}
const ๊ฐ๊ฐ์ ๋ = new Array("๊ฐ", "๊ฐ", "์ ", "๋");
i = 0;
while (i < ๊ฐ๊ฐ์ ๋.length) {
document.writeln("<li>" + ๊ฐ๊ฐ์ ๋[i] + "</li>");
i++;
}
i = 0;
let ๊ฐ๊ฐ์ ๋๋จ์ด = "";
while (i < ๊ฐ๊ฐ์ ๋.length) {
๊ฐ๊ฐ์ ๋๋จ์ด += ๊ฐ๊ฐ์ ๋[i];
i++;
}
document.write("<li>" + ๊ฐ๊ฐ์ ๋๋จ์ด + "</li>");
i = 0;
while (i < ๊ฐ๊ฐ์ ๋.length) {
document.write(๊ฐ๊ฐ์ ๋[i]);
i++;
}
ํจ์
- function ํจ์์ด๋ฆ() {~~@~~}
- ํจ์์ด๋ฆ();
- function a(){
document.write("Hello Function");
}
- a();
๋ฐ๋ณต๋ฌธ๊ณผ ๋ค๋ฅธ์ ์ a();๋ฅผ ์ด๋์์๋ ์จ๋ ๋๋ค. ์ถ๋ ฅ์ด ๋๋ค.
ํจ์์ ์ ๋ ฅ
- function a(input){
document.write(input);
}
a(2); : 2๋ฅผ ๋ฃ์ผ๋ฉด 2๊ฐ ๋์จ๋ค. 3์ ๋ฃ์ผ๋ฉด 3์ด ๋์จ๋ค. input์ผ๋ก ๋์ผํ๊ฒ ํ๋ค.
- function a(input){
document.write(input+1);
}
a(5); : a์ ์๋ 5๊ฐ input์ผ๋ก ๋ค์ด๊ฐ๋ค. ๊ทธ๋ฆฌ๊ณ 1์ด ๋ํด์ง๋ค.
ํจ์์ ์ถ๋ ฅ
- function a(input){
return input+1;
}
document.write(a(5)); : a์ ์๋ 5๊ฐ input์ผ๋ก ๋ค์ด๊ฐ๋ค. ๊ทธ๋ฆฌ๊ณ return ๊ฐ์ด ๋์ค๋ฉด ๊ทธ ๋ค์ ์๋ ๊ฐ์ด ์ถ๋ ฅ๋๋ค.6.
prompt(a(5)); : ์๋ฆผ์ฐฝ์ด ๋จ๋ฉด์ 6์ด ๋์จ๋ค. ์ถ๋ ฅ ํจ์์ด๋ค.
- prompt()๋ ๋ด์ฅ๋์ด์๋ ํจ์์ด๋ค. built-in function.
- write()๋ ๋ด์ฅ๋์ด์๋ ํจ์์ด๋ค.
- document. ๋ ๊ฐ์ฒด์ด๋ค. ์ฑ๊ฒฉ์ด ๋น์ทํ ํจ์๋ฅผ ๋ถ๋ฅํด์ ๋ฌถ์ด์ค๋ค.
JS ์ด๋ฒคํธ ํ๊ทธ ์ผ๋จ ์๋ต
css ํ์ฉํ์ฌ ์ฐ์ธก ์๋จ ๋ฐค/๋ฎ ์ ํ ๋ฒํผ ๋ง๋ค๊ธฐ
โถ CSS ํน์ ๋ ์ด์ด ๊ณ ์
<style type="text/css">
#originLayerStatic {position:static;}
#LeftTopLayerFixed {position:fixed;left:0;top:0}
#RightTopLayerFixed {position:fixed;right:0;top:0}
#LeftBottomLayerFixed {position:fixed;left:0;bottom:0}
#RightBottomLayerFixed {position:fixed;right:0;bottom:0}
</style>
์์์ ๋ถํฐ ์ข์ธก์๋จ, ์ฐ์ธก์๋จ, ์ข์ธกํ๋จ, ์ฐ์ธกํ๋จ ๊ณ ์ ์ํค๋ CSS
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link href="buttonstyle.css" rel="stylesheet" />
<script defer src="eventtag.js"></script>
</head>
<body>
<input type="text" id="msg" />
<div id="result"></div>
<input
id="inputbtn"
type="button"
value="Go!"
onclick="alert('test yet')"
/>
<div id="daynightbtn">
<input type="button" value="Day" />
<input type="button" value="Night" />
</div>
</body>
</html>
btn.css
#daynightbtn {
float: right;
position: fixed;
right: 10px;
top: 10px;
}
eventtag.js
//onfocus : https://www.w3schools.com/jsref/event_onfocus.asp
const msg = document.querySelector("#msg");
const result = document.querySelector("#result");
msg.onfocus = function (e) {
result.innerHTML = "onfocus ์ด๋ฒคํธ ๋ฐ๋";
};
msg.onblur = function (e) {
if (msg.value.length < 6) {
alert("์ต์ 6์๋ฆฌ์ฌ์ผ ํฉ๋๋ค!");
} else {
result.innerHTML = "ํฌ์ปค์ค ๋น ์ ธ๋๊ฐ ์ํ, ํ์ํฉ๋๋ค! Hello World!";
}
};
<head>
<script defer src="main.js"></script>
</head>
defer ์์ฑ์ ์ฌ์ฉํ๋ฉด,
๋ฐ๋๊น์ง ๋ ๋๋ง ํ ํ์, js ์ฝ๋๋ฅผ ์คํํ๋ค. ๋ฐ๋ผ์ ํด๋น js ์์ Html ๋ฐ๋์ ์ ๊ทผํ๋ ๊ฒ์ด ์ ๋ถ ๊ฐ๋ฅ!
-HTMLํ์ผ ํ์ฑํ๋ค๊ฐ (script ๋ง๋๋ฉด) ๋ณ๋ ฌ๋ก JS ๊ฐ์ด ๋ค์ด, (HTML ํ์ฑ ์๋ฃ ํ) JS ์คํ
-HTML ํ์ฑํ๋ ๋์ (JS๊ฐ ์ฌ๋ฌ๊ฐ๋ผ๋) ๋ณ๋ ฌ JS ๋ค์ด, ์ดํ์ ์ฌ๋ฌ๊ฐ JS๋ฅผ ์์๋๋ก ์คํ
use strict
- ํ์ ์คํฌ๋ฆฝํธ๋ฅผ ํ ๋๋ ์ ํ ์ ์ธํ ํ์๊ฐ ์์ง๋ง,
- ์์ ๋ฐ๋๋ผ ์๋ฐ์คํฌ๋ฆฝํธ ์ฌ์ฉํ ๋ 'use strict'; ์ ์ธํ๋ ๊ฒ์ด ์ข๋ค.
- JS๋ ๋งค์ฐ ์ ์ฐํ(๊ณ ๋ก ์ํํ) ์ธ์ด์ด๊ธฐ ๋๋ฌธ์ด๋ค
- ์ดํฌ๋ง์คํฌ๋ฆฝํธ5์ ๋ฐ๋ฅธ๋ค๋ ์๋ฏธ (์์์ ์ธ ๋ฒ์์์์)
<my ์ค์ต>
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="prompt.js" defer></script>
<script src="hello.js" defer></script>
</head>
<body></body>
</html>
prompt.js
"use strict";
const password = prompt("๋น๋ฐ๋ฒํธ");
if (password == "1111") {
document.write("๋น๋ฐ๋ฒํธ๋ฅผ ์ ๋๋ก ์
๋ ฅํ์
จ์ต๋๋ค.");
} else {
document.write("ํ๋ฆฐ ๋น๋ฐ๋ฒํธ!!!");
}
hello.js
const hello = "์๋
ํ์ธ์.";
document.body.innerText = hello;
'Javascript > Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋๋ฆผ์ฝ๋ฉ ์๋ฐ์คํฌ๋ฆฝํธ]๋น๋๊ธฐ์ ์์ ์ฝ๋ฐฑ์ง์ฅ (0) | 2022.04.13 |
---|---|
[์๋ฐ์คํฌ๋ฆฝํธ ์ค๊ธ] call, apply, bind (0) | 2022.03.28 |
[์ค๊ธ ์๋ฐ์คํฌ๋ฆฝํธ] ์ฌ๋ณผ (0) | 2022.03.24 |
[์ค๊ธ ์๋ฐ์คํฌ๋ฆฝํธ] ๊ฐ์ฒด ์ฐธ์ด, ๊ฐ์ฒด ๋ณต์ฌ, ๋ฐฐ์ด ์ปจํธ๋กค ์ฌํ (0) | 2022.03.24 |
[์๋ฐ์คํฌ๋ฆฝํธ] ๋์ ์์ฑ (0) | 2022.03.23 |