Bibi's DevLog ๐Ÿค“๐ŸŽ

[์ธํ”„๋Ÿฐ] Javascript์ž…๋ฌธ - DOM๊ณผ HTML (์š”์†Œ/ํƒœ๊ทธ/์—˜๋ฆฌ๋จผํŠธ, ์†์„ฑ, ๋ธ”๋ก ํƒœ๊ทธ/์ธ๋ผ์ธ ํƒœ๊ทธ, html๊ณผ JS ๋ถ„๋ฆฌ, JS๋กœ DOM ์ œ์–ดํ•˜๊ณ  ๊ฐ’ ๋ฐ”๊พธ๊ธฐ) ๋ณธ๋ฌธ

ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Javascript ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

[์ธํ”„๋Ÿฐ] Javascript์ž…๋ฌธ - DOM๊ณผ HTML (์š”์†Œ/ํƒœ๊ทธ/์—˜๋ฆฌ๋จผํŠธ, ์†์„ฑ, ๋ธ”๋ก ํƒœ๊ทธ/์ธ๋ผ์ธ ํƒœ๊ทธ, html๊ณผ JS ๋ถ„๋ฆฌ, JS๋กœ DOM ์ œ์–ดํ•˜๊ณ  ๊ฐ’ ๋ฐ”๊พธ๊ธฐ)

๋น„๋น„ bibi 2020. 10. 22. 15:13
* ์ด ๊ธ€์€ ์ธํ”„๋Ÿฐ์—์„œ ์ œ๊ณตํ•˜๋Š” ํ˜ธ๋ˆ…์Šค๋‹˜์˜ ์œ ๋ฃŒ ๊ฐ•์˜ '์‰ฝ๊ณ  ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ฐฐ์›Œ๋ณด๋Š” Javascript ์ž…๋ฌธ - ์ฝ”๋“œ์Šค์ฟผ๋“œ ๋งˆ์Šคํ„ฐ์ฆˆ ์ฝ”์Šค ๋ ˆ๋ฒจ1'๋ฅผ ๋“ฃ๊ณ  ๊ณต๋ถ€ํ•˜๋ฉฐ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ๊ฐ•์˜ ๋‚ด์šฉ์— ๋”ํ•ด, ์ œ๊ฐ€ ํ•„๊ธฐํ•˜๊ณ  ๊ตฌ๊ธ€๋งํ•œ ๋‚ด์šฉ์ด ์ •๋ฆฌ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ดˆ๋ณด์ธ ์ œ๊ฐ€ ์ดํ•ดํ•œ ๋Œ€๋กœ ์ •๋ฆฌํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋ถ€์ •ํ™•ํ•œ ๋‚ด์šฉ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Œ์„ ๊ฐ์•ˆํ•ด ์ฃผ์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ๊ธ€์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ธํ”„๋Ÿฐ ์ธก ๋‹ต๋ณ€์„ ๋ฐ›์€ ์ ์ด ์žˆ์œผ๋‚˜(https://bibi6666667.tistory.com/37), ์ €์ž‘๊ถŒ์ƒ ๋ฌธ์ œ๊ฐ€ ๋œ๋‹ค๋ฉด ๋ณดํ˜ธ ๋ชจ๋“œ(๋น„๊ณต๊ฐœ)๋กœ ์ „ํ™˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

DOM๊ณผ HTML

HTML

  • HyperText Markup Language ์˜ ์•ฝ์ž
  • WWW(World Wide Web)์˜ 3์š”์†Œ ์ค‘ ํ•˜๋‚˜.
    • HTML (์›น ๋ฌธ์„œ)
    • URL (์›น ๋ฌธ์„œ์˜ ์ฃผ์†Œ)
    • HTTP (์›น ๋ฌธ์„œ๋ฅผ ์ฃผ๊ณ ๋ฐ›๋Š” ํ†ต์‹ ๊ทœ์น™)
  • ํŒ€ ๋ฒ„๋„ˆ์Šค๋ฆฌ์— ์˜ํ•ด ๋งŒ๋“ค์–ด ์กŒ๋‹ค. (WWW์™€ ํ•จ๊ป˜ ๋งŒ๋“ค์–ด์ง)
  • ์ธํ„ฐ๋„ท = ๋„คํŠธ์›Œํฌ๋ง(๋ฏธ๊ตญ ๊ตญ๋ฐฉ์„ฑ์—์„œ ๋งŒ๋“ฆ)
    ์ธํ„ฐ๋„ท์„ ํ†ตํ•ด ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ ๋ณด์ด๋Š” ํ™”๋ฉด์ด WWW์ด๋‹ค.

 

์š”์†Œ (์—˜๋ฆฌ๋จผํŠธ, Element) (=ํƒœ๊ทธ)

  • HTML์—์„œ์˜ ์š”์†Œ ๋‹จ์œ„๋Š” ํƒœ๊ทธtag ์ด๋‹ค.
  • ์—ฌ๋Š” ํƒœ๊ทธ<>์™€ ๋‹ซ๋Š” ํƒœ๊ทธ</>๋กœ ์ด๋ฃจ์–ด์ง.
    • ๋‹ซ๋Š” ํƒœ๊ทธ๊ฐ€ ์—†๋Š” ํƒœ๊ทธ๋„ ์žˆ๋‹ค. (img, br, input)
  • ํƒœ๊ทธtag๋Š” '์†์„ฑattributes' ๊ณผ '์ปจํ…์ธ contents'๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
    • ํƒœ๊ทธ ์‚ฌ์ด์— ๋“ค์–ด์˜ค๋Š” ๊ฐ’์€ '์ปจํ…์ธ contents'
    • ํƒœ๊ทธ ์•ˆ์— ๋“ค์–ด์˜ค๋Š” ๊ฐ’์ด '์†์„ฑattributes'๊ณผ '์†์„ฑ๊ฐ’'
์˜ˆ๋ฅผ ๋“ค์–ด
<h2 id="header"> hello </h2>
์—์„œ..
<h2> : ์—ฌ๋Š” ํƒœ๊ทธ
</h2> : ๋‹ซ๋Š” ํƒœ๊ทธ
hello : ์ปจํ…์ธ 
id : ์†์„ฑ
"header" : (์†์„ฑ id์˜) ์†์„ฑ๊ฐ’

 

์†์„ฑ(Attributes)

๊ฐ์ฒด์˜ ์†์„ฑ๊ณผ ๋น„์Šทํ•˜๊ฒŒ, ์—˜๋ฆฌ๋จผํŠธ(ํƒœ๊ทธ)๋„ ์†์„ฑ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โž• ๊ฐ์ฒด์™€ ์—˜๋ฆฌ๋จผํŠธ(ํƒœ๊ทธ)๋Š” ์ƒ๋‹นํžˆ ์œ ์‚ฌํ•˜๋‹ค.

<a href="http://codesquad.kr" title="์ฝ”๋“œ์Šค์ฟผ๋“œ">
<a> : ์—˜๋ฆฌ๋จผํŠธ=ํƒœ๊ทธ
href : ์†์„ฑ
"http://codesquad.kr" : (์†์„ฑ href์˜) ์†์„ฑ๊ฐ’
title : ์†์„ฑ
"์ฝ”๋“œ์Šค์ฟผ๋“œ" : (์†์„ฑ title์˜) ์†์„ฑ๊ฐ’

 

๋ธ”๋ก ์—˜๋ฆฌ๋จผํŠธ(๋ธ”๋ก ํƒœ๊ทธ Block Tag)

(์‚ฌ์šฉํ–ˆ์„ ๋•Œ) p, h1, div .. ์ฒ˜๋Ÿผ ์ค„์ด ๋ฐ”๋€Œ๋Š” ์—˜๋ฆฌ๋จผํŠธ(ํƒœ๊ทธ)

์ธ๋ผ์ธ ์—˜๋ฆฌ๋จผํŠธ(์ธ๋ผ์ธ ํƒœ๊ทธ Inline Tag)

(์‚ฌ์šฉํ–ˆ์„ ๋•Œ) span, img, input, button, a .. ์ฒ˜๋Ÿผ ์ค„๋ฐ”๋€œ์ด ์—†๋Š” ์—˜๋ฆฌ๋จผํŠธ(ํƒœ๊ทธ)

 

HTML๊ณผ JS ๋ถ„๋ฆฌํ•˜๊ธฐ

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<h2>Hello</h2>
<p id="main">This is p element</p>
<script>
    // ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‚ด์šฉ
</script>
</body>
</html>

์ง€๊ธˆ๊นŒ์ง€๋Š” html์•ˆ์— javascript๋ฅผ ๋„ฃ์–ด์„œ ์ผ์ง€๋งŒ, ์ด์ œ html๊ณผ javascript๋ฅผ ๋ถ„๋ฆฌํ•ด ๋ณผ ๊ฒƒ์ด๋‹ค.
๋‘ ํŒŒ์ผ์„ ๋ถ„๋ฆฌํ•ด ์ข€ ๋” ๊น”๋”ํ•˜๊ฒŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.

dom.html (๋ถ„๋ฆฌํ•œ htmlํŒŒ์ผ)

htmlํŒŒ์ผ์—์„œ <script></script> ์‚ฌ์ด์— ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‚ด์šฉ์„ ๋„ฃ๋Š” ๋Œ€์‹ ,
<script src="์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ ๊ฒฝ๋กœ"></script> ๋กœ ์ ์–ด์ค€ ๋’ค
JSํŒŒ์ผ์„ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด์„œ ๊ฑฐ๊ธฐ์— ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ ๋„ฃ๋Š”๋‹ค.

*src = source์˜ ์ค„์ธ๋ง.
<!DOCTYPE html>
<meta charset="UTF-8">
<html>
<title>HTML Tutorial</title> 
<head> 
</head> 
<body>

<h1>This is a heading</h1>
<h1>Another h1 heading</h1> 
<p id='main'>This is a paragraph.</p> 

<script src="dom_test.js"></script> 
</body> 
</html>

 

dom_test.js (๋ถ„๋ฆฌํ•œ JS ํŒŒ์ผ)

console.log("Hello JS");

 

DOM (Document Object Model, ๋ฌธ์„œ ๊ฐ์ฒด ๋ชจ๋ธ)

  • HTML๋ฌธ์„œ์˜ "์š”์†Œelement"๋“ค์„ "๊ฐ์ฒดobject"๋ฅผ ์ด์šฉํ•ด์„œ ๊ณ„์ธต ๊ตฌ์กฐ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•.
    * ๊ณ„์ธต ๊ตฌ์กฐ hierarchy structure : ์กฑ๋ณด์™€ ๊ฐ™์€ ๋ถ€๋ชจ-์ž์‹์˜ ํŠธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ๊ณ„์ธต๊ตฌ์กฐ๋ผ๊ณ  ์นญํ•จ. (= ํŠธ๋ฆฌ ๊ตฌ์กฐ)
  • DOM์˜ ํ‘œ์ค€: W3CDOM (DOM ํ‘œ์ค€์œ„์›ํšŒ)
  • DOM ๊ตฌํ˜„์ฒด: ์›น ๋ธŒ๋ผ์šฐ์ €. Gecko(๊ฒŒ์ฝ” - ๋ชจ์งˆ๋ผ ํŒŒ์ด์–ดํญ์Šค์—์„œ ์‚ฌ์šฉ), Webkit(์• ํ”Œ์—์„œ ๋งŒ๋“ฆ) ๋“ฑ

 

html ๊ฐ์ฒด

HTML ๋ฌธ์„œ ์ž์ฒด๋„ "๊ฐ์ฒด"๋กœ ๊ฐ„์ฃผ๋œ๋‹ค.

  • window: ์ตœ์ƒ์œ„ ๊ฐ์ฒด!
  • document: dom์˜ ์ตœ์ƒ์œ„ ๊ฐ์ฒด์ด๋ฉด์„œ window์˜ ํ•˜์œ„ ๊ฐ์ฒด.
    (์›๋ž˜๋Š” window.document๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•˜์ง€๋งŒ, ๋‘˜์€ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์— document๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ)
    (window.document === document; // true)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<h1>Big title</h1>
<h2>Hello 1</h2>
<p id="main">This is p element</p>
<h2>Hello 2</h2>
<script src="dom_test.js">
    // ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‚ด์šฉ
</script>
</body>
</html>

Q. ์œ„ ๋ฌธ์„œ์˜ ๊ณ„์ธต๊ตฌ์กฐ๋Š”?
((↑ ์ƒ์œ„))
html
window
document
h1 (1๊ฐœ)
h2 (1๊ฐœ)
p (1๊ฐœ)
h2 (1๊ฐœ)
((↓ ํ•˜์œ„))

var list = document.getElementById('main');
main.innerHTML = "Hello";

 

JS๋กœ DOM ์ œ์–ดํ•˜๊ธฐ

๋ชจ๋“  HTML ์—˜๋ฆฌ๋จผํŠธ๋Š” ๊ฐ์ฒด์ด๋ฏ€๋กœ ๋‹ค๋ฅธ ๊ฐ์ฒด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ JS๋กœ ์ œ์–ด๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

var a = document.getElementsByTagName('h2'); 
// ()๋‚ด์˜ ํƒœ๊ทธ ์ด๋ฆ„์„ ๊ฐ€์ง„ ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์„œ๋“œ.
// get"Elements"ByTagName()์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ™์€ ํƒœ๊ทธ์ด๋ฆ„์˜ ๋ชจ๋“  ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

var b = document.getElementById("");
// id๊ฐ’์ด ""์ธ ๊ฐ์ฒด ํ•˜๋‚˜๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์„œ๋“œ. 
// id๋Š” uniqueํ•˜๋ฏ€๋กœ ์–ธ์ œ๋‚˜ ํ•˜๋‚˜์ด๋‹ค. ๋”ฐ๋ผ์„œ get"Element"ById()

a; // HTMLCollection(2) [h2, h2]
a[0]; // <h2>Hello 1</h2>
a[1]; // <h2>Hello 2</h2>

//.innerHTML : ํƒœ๊ทธ ๋‚ด์˜ ์ปจํ…์ธ ๋ฅผ ๊ฐ€๋ฆฌํ‚ด.
a[0].innerHTML;  // "Hello 1"
a[0].innerHTML = "Hello"; // ์›น๋ฌธ์„œ์˜ ์ฒซ ๋ฒˆ์งธ h2 ๋‚ด์šฉ์ด 'Hello 1' -> 'Hello'๋กœ ๋ฐ”๋€œ.

DOM ๊ฐ์ฒด์˜ ๊ฐ’ ๋ฐ”๊พธ๊ธฐ

 

var element = document.getElementById('id')
element.innerHTML = '๊ฐ’'

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<h1>Big title</h1>
<h2>Hello 1</h2>
<p id="main">This is p element</p>
<h2>Hello 2</h2>
<script src="dom_test.js"></script>
</body>
</html>

// ์œ„ html๋ฌธ์„œ์—์„œ JS์—์„œ ์ด๋ ‡๊ฒŒ ์ œ์–ดํ•˜๋ฉด
<script>
var p = document.getElementById("main");
p; // <p id="main">This is p element</p>
p.innerHTML = "๋‚˜๋Š” pํƒœ๊ทธ์ž…๋‹ˆ๋‹ค"; // <p id="main">๋‚˜๋Š” pํƒœ๊ทธ์ž…๋‹ˆ๋‹ค</p> ๋กœ ๋ฌธ์„œ ๋‚ด์šฉ์ด ๋ฐ”๋€œ.
</script>

 

์ฐธ์กฐ ๊ฐ•์ขŒ ) ์ƒํ™œ์ฝ”๋”ฉ WEB1 - HTML & Internet
opentutorials.org/course/3084

 

WEB1 - HTML & Internet - ์ƒํ™œ์ฝ”๋”ฉ

--- ์šฐ๋ฆฌ๋Š” ์ง€๊ธˆ๋ถ€ํ„ฐ ์ฝ”๋”ฉ ์›น ์ธํ„ฐ๋„ท ์ปดํ“จํ„ฐ๋ผ๋Š” ๊ฑฐ๋Œ€ํ•œ ์ฃผ์ œ์— ๋Œ€ํ•œ ํƒํ—˜์„ ์‹œ์ž‘ํ•  ๊ฑฐ์˜ˆ์š”. ์ด ์—ฌํ–‰์„ ์‹œ์ž‘ํ•˜๊ธฐ์— ์•ž์„œ์„œ ํ•œ๊ฐ€์ง€ ์ค€๋น„๊ฐ€ ํ•„์š”ํ•œ๋ฐ์š”. ๋ฐ”๋กœ ์šฐ๋ฆฌ๋“ค์˜ ์ƒ์ƒ๋ ฅ์ž…๋‹ˆ๋‹ค. ์ง€๊ธˆ๋ถ€ํ„ฐ ์—ฌ

opentutorials.org

์ด ๊ฐ•์ขŒ๋ฅผ ํ•จ๊ป˜ ๋“ค์œผ๋ฉด html๊ณผ ์ธํ„ฐ๋„ท์— ๋Œ€ํ•ด ๋” ๊นŠ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค.