DOM
์ด๋ Document Object Model
= ๋ฌธ์ ๊ฐ์ฒด ๋ชจ๋ธ
์ด๋ผ๊ณ ํด์ํ ์ ์๋ค.
<html>
์ด๋ <body>
๊ฐ์ html์ ํ๊ทธ๋ค์ JavaScript๊ฐ ์ด์ฉํ ์ ์๋ ๊ฐ์ฒด(object)๋ก ๋ง๋ ๊ฒ์ ์๋ฏธํ๊ณ ,์ฆ, DOM(Document Object Model, ๋ฌธ์ ๊ฐ์ฒด ๋ชจ๋ธ)์ด๋
JavaScript๊ฐ ์ด์ฉํ ์ ์๋ ๊ฐ์ฒด(object)๋ก ๋ง๋<html>
์ด๋<body>
๊ฐ์ html๋ฌธ์์ ํ๊ทธ๋ค์ ์ธ์ํ๊ฑฐ๋ ํด์ํ๋ ๋ฐฉ๋ฒ์ด๋ค.
DOM์ tree ํ์์ ์๋ฃ ๊ตฌ์กฐ๋ก ๋์ด ์๋ค.
์๋์ ๊ฐ์ ์น๋ฌธ์์ DOM์ tree ๊ตฌ์กฐ๋ฅผ ํ์ธํด๋ณด์
<!DOCTYPE html>
<html>
<head>
<title>DOM์ด๋?</title>
</head>
<body>
<h1 class="title">์ ๋ชฉ ์์ญ</h1>
<p class="content">๋ณธ๋ฌธ ์์ญ</p>
</body>
</html>
์ ์ฝ๋์ tree ๊ตฌ์กฐ๋ ์๋์ ๊ฐ์ด ๋ํ๋ผ ์ ์๋ค.
DOM์ tree ๊ตฌ์กฐ๋ ์์ ๊ฐ์ด ๋
ธ๋์ ๊ฐ์ง๋ก ํํํ๋ค.
๊ฐ ๋ฐ์ค๊ฐ ๋
ธ๋์ด๊ณ ๋
ธ๋์ ๋
ธ๋ ์ฌ์ด๋ฅผ ์ฐ๊ฒฐํ์ฌ ๊ด๊ณ๋ฅผ ํ์ํ๊ณ ์๋ ์ ์ด ๊ฐ์ง์ด๋ค.
์ tree ๊ตฌ์กฐ์์
<body>
ํ๊ทธ๋ฅผ ์๋์ <h1>
๊ณผ <p>
์ ๋ถ๋ชจ ์์๋ก ์ธ์ํ๊ณ , <h1>
๊ณผ <p>
ํ๊ทธ๋ <body>
์ ์์ ์์๋ก ์ธ์ํ๋ค๐ treeํ ์๋ฃ ๊ตฌ์กฐ๋?
- ํ๋์
Root node
(=๋ฟ๋ฆฌ)์์ ์์๋๋ค.- tree์์๋ ํ๋์ ๋ ธ๋๋ฅผ ๊ธฐ์ค์ผ๋ก ์์ชฝ์ ์์นํ ๋ ธ๋๋ฅผ
parent node
(=๋ถ๋ชจ),์๋ซ์ชฝ ๋ ธ๋๋ฅผchild node
(=์์)๋ผ๊ณ ํ๋ค.Root node
๋ ๊ฐ์ฅ ์์์ ์์๋๋ node์ด๊ธฐ ๋๋ฌธ์parent node
(=๋ถ๋ชจ)๊ฐ ์๋ node๊ฐ ๋๊ณ , ๋ฐ๋๋ก children(์์)์ด ์๋ node๋ฅผLeaf node
(=์) ๋ผ๊ณ ๋ ํ๋ค.- ์ด๋ฌํ treeํ ์๋ฃ๊ตฌ์กฐ๋ ๋ฟ๋ฆฌ(root)์์ ์์ํด์ ์(leaf)์์ ๋๋๋ ๊ตฌ์กฐ์ด๋ค!
๐ node(๋ ธ๋) ๋?
- tree ๊ตฌ์กฐ์์
Root node
๋ฅผ ํฌํจํ ๊ฐ๊ฐ์ ๊ฐ์ฒด๋ฅผ node๋ผ๊ณ ํํํ๋ค.
= ๊ทธ๋ฆผ์์๋ ๋ฐ์คํ๋๊ฐ node๋ฅผ ์๋ฏธํ๋ค<head>
,<body>
,<title>
,<script>
,<h1>
๋ฑ์ ํ๊ทธ๋ฟ ์๋๋ผ ํ๊ทธ ์์ ํ ์คํธ๋ ์์ฑ ๋ฑ๋ ๋ชจ๋ node์ ์ํ๋ค.
DOM์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ฌ๋ฌ ํ๋ก๊ทธ๋จ๋ค์ด ํ์ด์ง์ ์ฝํ ์ธ ๋ฐ ๊ตฌ์กฐ, ๊ทธ๋ฆฌ๊ณ ์คํ์ผ์ ์ฝ๊ณ ์กฐ์ํ ์ ์๋๋ก API๋ฅผ ์ ๊ณตํ๋ฉฐ, ๋ฌธ์ ๋ด์ ๋ชจ๋ ์์๋ฅผ ์ ์ํ๊ณ , ํด๋น ์์์ ์ ๊ทผํ๋ ๋ฐฉ๋ฒ๊น์ง ์ ์ํ๋ค.
JavaScript๋ DOM์ ํตํด ์น ํ์ด์ง์ ์ ๊ทผํ๊ณ , ํ์ด์ง๋ฅผ ์์ ํ ์ ์์ผ๋ฉฐ ๋ํ ์ด์ฉ์์ ํ๋์ ๋ฐ๋ผ HTML, CSS๋ฅผ ๋ณ๊ฒฝํ๋ฉฐ ์ด์ฉ์์ ์ํธ์์ฉ๋ ๊ฐ๋ฅํ๋ค.
๐ก๊ฒฐ๊ณผ์ ์ผ๋ก DOM์ web page๋ฅผ ๋ณด๋ค ๋ ๋์ ์ผ๋ก(interactive) ์ฌ์ฉ ๊ฐ๋ฅํ๊ฒ ํ๊ธฐ ์ํด์ browser๊ฐ ๋ง๋ ๊ฐ์ฒด๋ผ๊ณ ์ดํดํ ์ ์๋ค.
์ฃผ์ํด์ผํ ์ ์ DOM์ ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ์๋๋ผ๋ ์ ์ด๋ค.
DOM์ ์น ๋ฌธ์์์๋ง ์กด์ฌํ๊ณ , ๋ธ๋ผ์ฐ์ ๊ฐ ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์ด์ฉํด์ ์น ํ์ด์ง๋ฅผ ์กฐ์ํ ์ ์๋๋ก ์ ๊ณตํด์ฃผ๋ ํ๋์ ์์์ผ ๋ฟ์ด๋ค.
์ฐ๋ฆฌ๊ฐ ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์ด์ฉํด ์น ํ์ด์ง๋ฅผ ์กฐ์ํ๋ ๊ฒ์ ์๋ณธ html ๋ฌธ์ ์์ฒด๋ฅผ ์์ ํ๊ฑฐ๋ ์กฐ์ํ๋ ๊ฒ์ด ์๋๋ผ DOM์ ์กฐ์ํ๋ ๊ฒ์ด๋ค.
๐ ์ฐธ๊ณ
http://www.tcpschool.com/xml/xml_dom_concept
https://m.blog.naver.com/magnking/220972680805
https://im-developer.tistory.com/100