* ํด๋น ํฌ์คํ ์ 1๋ถ ์ฝ๋ฉ๋์ ์ ํ๋ธ ๋ฌด๋ฃ ๊ฐ์๋ฅผ ์ฐธ๊ณ ํ์ฌ ํ์ต, ์์ฑํ์์ต๋๋ค*
part1,2 ๋ด์ฉ
<!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 rel="stylesheet" href="style.css" />
</head>
<body>
<script>
//์์ฑ์(constructor)
function Person(username, age) {
this.username = username;
this.age = age;
this.introduce = function () {
console.log(
"์๋
ํ์ธ์. ์ ์ด๋ฆ์ " +
this.username +
"์ด๊ณ , ์ ๋์ด๋ " +
this.age +
"์
๋๋ค"
);
};
}
//์ธ์คํด์ค(instance)
const ์ฒ ์ = new Person("์ฒ ์", 6);
const ์งฑ๊ตฌ = new Person("์งฑ๊ตฌ", 6);
์ฒ ์.introduce();
์งฑ๊ตฌ.introduce();
</script>
</body>
</html>
DOM๊ณผ Event๊น์ง ํ๋ฉด ๋์ ์ธ ํ์ด์ง๋ฅผ ๊ตฌ์ฑํ๋ ๊ฒ์ด ๊ฐ๋ฅํด์ง๋ค!
์น HTML ํ๊ทธ๋ค์ ์ง๋ค๋ณด๋ฉด ํธ๋ฆฌ๊ตฌ์กฐ๊ฐ ํ์ฑ์ด ๋๊ณ ,
์ด ํธ๋ฆฌ๊ตฌ์กฐ๋ฅผ ์ด๋ฃจ๋ ์์ ๊ฐ๊ฐ ํ๋ํ๋๊ฐ ์ ๋ถ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด์ด๋ค.
(ํ๊ทธ ๋ฟ๋ง ์๋๋ผ ํ ์คํธ๋ ๋ง์ฐฌ๊ฐ์ง ์ทจ๊ธ... ์ฌ์ง์ด ํด๋์ค๋ ID, ๋๋ ํ์ดํผ๋งํฌ๋ img src ๊ฐ์ ์์ฑ ๊ทธ๋ฐ ์ ๋ค๋.. DOM ๊ฐ์ฒด์ ์ํ๋ค ๊ฒฐ๊ตญ DOM ์คํฌ๋ฆฝํธ๋ html element๋ฅผ ์๋ฐ์คํฌ๋ฆฝํธ๋ก ์กฐ์ํ๋ ๊ฒ์ ๋งํ๋ค.)
<!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 rel="stylesheet" href="style.css" />
<script>
const title = document.getElementById("main-title");
console.log(title);
</script>
</head>
<body>
<h1 id="main-title">DOM(Document Object Model)</h1>
<p>DOM์ด๋, ๋ฌธ์์ ์๋ ๊ฐ์ฒด๋ค์ ๋ฐ๋ผ๋ณด๋ ๋ชจ๋ธ</p>
<article>
<header>header</header>
<section>
<header>s header</header>
section 1
</section>
<section>
<header>s header</header>
section 2
</section>
</article>
</body>
</html>
์์ ๊ฐ์ด ์์ฑํ๋ฉด ์ฝ์์ title์ null์ด ๋ ๊ฒ์ด๋ค. ์๋ํ๋ฉด html ์์๋ค์ ์์ง ์ฝ์ง๋ ์์๊ธฐ ๋๋ฌธ์ด๋ค.(์์์ ์๋๋ก ๋ฒ์น์ ์๊ฑฐํ์ฌ)
ํน์ง
1. head ์์ DOM์คํฌ๋ฆฝํธ๋ฅผ ์์ฑํ ๋๋ html ๋ฌธ์๊ฐ ๋ค ์ฝํ ๋๊น์ง ๊ธฐ๋ค๋ ธ๋ค๊ฐ ์คํํด์ฃผ์ด์ผ ํ๋ค.
๋ฐ๋ผ์ window.onload = function(){} ์ ๋์์ ๋ฐ์์ผ ํ๋ค!
- window๋ ์๋ฐ์คํฌ๋ฆฝํธ์ ์ ์ญ ๊ฐ์ฒด
- onload๋ ์ด๋ฒคํธ(์ข.. ๊ตฌ์.. ์๋ ์คํ์ผ^^;)
- window.onload = function(){์๋ฌด ํจ์}๋ป : "์ ์ฒด ๋ฌธ์๊ฐ ๋ก๋๊ฐ ๋๋ฉด ๊ดํธ์์ ๋ค์ด๊ฐ ์๋ฌด ํจ์๋ฅผ ์คํํด์ฃผ์ธ์."
<!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 rel="stylesheet" href="style.css" />
<script>
window.onload = function () {
const title = document.getElementById("main-title");
console.log(title);
};
</script>
</head>
<body>
<h1 id="main-title">DOM(Document Object Model)</h1>
<p>DOM์ด๋, ๋ฌธ์์ ์๋ ๊ฐ์ฒด๋ค์ ๋ฐ๋ผ๋ณด๋ ๋ชจ๋ธ</p>
<article>
<header>header</header>
<section>
<header>s header</header>
section 1
</section>
<section>
<header>s header</header>
section 2
</section>
</article>
</body>
</html>
window.onload = function(){์๋ฌด ํจ์} //์์ฑ์ ๊ฐ ํ ๋น
๋์ ์ ์์ฆ ์ํ์ผ๋ก ์์ฑํด๋ณด์.
๋์์ ๋๊ฐ๋ค~(๊ฐ์ฒด์ ์์ฑ ์ค์ ๊ฐ์ด ํจ์์ ์ ๋ค์ ํน๋ณํ '๋ฉ์๋'๋ก ๋ถ๋ฅด๊ธฐ๋ก ํ๋ค.)
<!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 rel="stylesheet" href="style.css" />
<script>
window.addEventListener("load", function () {
const title = document.getElementById("main-title");
console.log(title);
});
</script>
</head>
<body>
<h1 id="main-title">DOM(Document Object Model)</h1>
<p>DOM์ด๋, ๋ฌธ์์ ์๋ ๊ฐ์ฒด๋ค์ ๋ฐ๋ผ๋ณด๋ ๋ชจ๋ธ</p>
<article>
<header>header</header>
<section>
<header>s header</header>
section 1
</section>
<section>
<header>s header</header>
section 2
</section>
</article>
</body>
</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 rel="stylesheet" href="style.css" />
<script>
/*window.addEventListener("load", function () {
const title = document.getElementById("main-title");
console.log(title);
});*/
window.addEventListener("DOMContentLoaded", function () {
const title = document.getElementById("main-title");
console.log(title);
});
</script>
</head>
<body>
<h1 id="main-title">DOM(Document Object Model)</h1>
<p>DOM์ด๋, ๋ฌธ์์ ์๋ ๊ฐ์ฒด๋ค์ ๋ฐ๋ผ๋ณด๋ ๋ชจ๋ธ</p>
<article>
<header>header</header>
<section>
<header>s header</header>
section 1
</section>
<section>
<header>s header</header>
section 2
</section>
</article>
</body>
</html>
์ฐจ์ด์ ์ ์์๋ณด์
/*window.addEventListener("load", function () {
const title = document.getElementById("main-title");
console.log(title);
});*/
window.addEventListener("DOMContentLoaded", function () {
const title = document.getElementById("main-title");
console.log(title);
});
load๋ ์ ์ฒด๋ด์ฉ์ด ๋ค~ ๋ก๋๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฐ๋ค.(๋ฌธ์ ๋ด์ฉ์ธ๋ ์ด๋ฏธ์ง๊ฐ ๋ฌด๊ฒ๊ณ ๋ง์ผ๋ฉด ์๊ฐ์ด ๊ฑธ๋ฆฐ๋ค. src ์์ ์ธ์ธํ ์ด๋ฏธ์ง ๋ฐ์ดํฐ๊ฐ ๋ค ๋ก๋๋์ด์ผ ํ๋ค๋ ๋ป.)
DOMContentLoaded๋ DOM์ Content๋ง ๋ก๋๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฐ๋ค. (์ฆ ์ต๊ฐํ html ๊ตฌ์กฐ ํ๊ทธ๋ง ๋ค ๋ก๋๋๋ฉด ๋๋๊น ์๋๊ฐ ๋น ๋ฅด๋ค. src๊ฐ ์๋ค๋ ๊ฒ๋ง ์ฝํ๋ฉด, ์ฆ src ๋งํฌ ๋ฐ์ดํฐ ๋ก๋ฉ์ ๊ตณ์ด ๊ธฐ๋ค๋ฆฌ์ง ์๋๋ค๋ ๋ป.)
script๋ฅผ head๊ฐ ์๋๋ผ body ๋ซ๊ธฐ ์ง์ ์ ํ๋ฉด ์์ ์์ ์ด ๊ตณ์ด ํ์ ์์ด์ง๋ค.
์ด๋ฏธ html ์ฝ๋๋ฅผ ๋ค ์ฝ๊ณ ์๊ธฐ ๋๋ฌธ์ ๊ธฐ๋ค๋ฆด ํ์๊ฐ ์๋ ๊ฒ.
๊ทธ๋์ ์ฌ์ค์ ์ถ์ธ๊ฐ ์คํฌ๋ฆฝํธ ํ๊ทธ๋ฅผ head๊ฐ ์๋๋ผ body ๋ซ๊ธด ์ง์ ์ ๋ฃ๋ ๊ฒ์ผ๋ก ๋ฐ๋์๋ค.
์ ์ ์ ์ฅ์์๋ ํ ์คํธ ํ๋๋ผ๋ ๋ ๋นจ๋ฆฌ ๋จ๋ ๊ฒ์ด ๋๋ฆฌ์ง ์๊ฒ ๋๊ปด์ง๋๊น...
๋ฌผ๋ก SPA, ์๋ฐ์คํฌ๋ฆฝํธ๋ก ํ๋ฉด์ ๋ค ๊ทธ๋ ค๋ด๋ ์น ์ดํ๋ฆฌ์ผ์ด์ ์ ํด๋น๋๋ ๋ด์ฉ ์๋
'Javascript > Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๋ฐ ์คํฌ๋ฆฝํธ] ์ฆ์์คํํจ์... ๊ทธ๋ฆฌ๊ณ ํด๋ก์ (0) | 2022.03.21 |
---|---|
[๊ธฐ์ด ์๋ฐ์คํฌ๋ฆฝํธ] ํ์ดํ ํจ์ (0) | 2022.03.21 |
[๊ธฐ์ด ์๋ฐ์คํฌ๋ฆฝํธ] alert, prompt, confitm (0) | 2022.03.20 |
[์๋ฐ์คํฌ๋ฆฝํธ ์์ ] ๋งค๊ฐ๋ณ์ ์์ ๋ฐ๋ฅธ Function return ์ ์ด (0) | 2022.03.15 |
[๊ธฐ์ด ์๋ฐ์คํฌ๋ฆฝํธ] 9.ํจ์, ๋งค๊ฐ ๋ณ์, ๋ฐํ (0) | 2022.03.14 |