Notice
Recent Posts
Recent Comments
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

[16] 230118 Ch. 10 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ DOM๊ณผ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ, ์ž…๋ ฅ ๊ฒ€์ฆ: 1. ๋ฌธ์„œ ๊ฐ์ฒด ๋ชจ๋ธ(DOM), 2. HTML ์š”์†Œ ์ฐพ๊ธฐ, 3. HTML ๋ณ€๊ฒฝํ•˜๊ธฐ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 16์ผ] ๋ณธ๋ฌธ

๐ŸŒณBootcamp Revision 2023โœจ/JavaScript, jQuery, Ajax, JSON

[16] 230118 Ch. 10 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ DOM๊ณผ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ, ์ž…๋ ฅ ๊ฒ€์ฆ: 1. ๋ฌธ์„œ ๊ฐ์ฒด ๋ชจ๋ธ(DOM), 2. HTML ์š”์†Œ ์ฐพ๊ธฐ, 3. HTML ๋ณ€๊ฒฝํ•˜๊ธฐ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 16์ผ]

yjyuwisely 2023. 1. 18. 12:50

230118 Wed 16th class

Ch. 9 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด (์ฃผ์ œ๋ณ„๋กœ ๋ฌถ๊ธฐ ์œ„ํ•ด ์•„๋žซ๊ธ€๋กœ ๊ธ€ ๋‚ด์šฉ ์˜ฎ๊น€.) 
Ch. 10 DOM๊ณผ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ, ์ž…๋ ฅ ๊ฒ€์ฆ
์ง„๋„: p. 366 ~ (๊ต์žฌ: HTML5 + CSS3 + JavaScript๋กœ ๋ฐฐ์šฐ๋Š” ์›นํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ์ดˆ, ์ €์ž: ์ฒœ์ธ๊ตญ)

 

HTML5+CSS3+JavaScript๋กœ ๋ฐฐ์šฐ๋Š” ์›นํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ์ดˆ | ์ฒœ์ธ๊ตญ - ๊ต๋ณด๋ฌธ๊ณ 

HTML5+CSS3+JavaScript๋กœ ๋ฐฐ์šฐ๋Š” ์›นํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ์ดˆ | ์›นํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ดˆ๋ณด์ž๋„ ์‰ฝ๊ฒŒ ๋ฐฐ์šฐ๋Š” ์›น ๊ฐœ๋ฐœ ์ง€์นจ์„œ!์ด ์ฑ…์€ ์›นํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ฒ˜์Œ ๋ฐฐ์šฐ๋Š” ์ดˆ๋ณด์ž๋“ค์„ ์œ„ํ•ด ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ฐจ๊ทผ์ฐจ๊ทผ ์„ค๋ช…ํ•œ๋‹ค. 1~3์žฅ์—

product.kyobobook.co.kr


์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๊ธฐ์–ตํ•  ๊ฒƒ์„ ์ •๋ฆฌํ–ˆ๋‹ค.


์ง€๋‚œ ์ˆ˜์—… ๋•Œ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๋‹ค์‹œ ๊ธฐ์–ตํ•  ๊ฒƒ

- ๋ฆฌํ„ฐ๋Ÿด์€ ๋ณ€์ˆ˜์˜ ๊ฐ’์ด ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ(๋ฉ”๋ชจ๋ฆฌ ์œ„์น˜ ์•ˆ์˜ ๊ฐ’)๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

- ๋ณ€์ˆ˜ ์„ ์–ธ์—๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ const๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์žฌํ• ๋‹น(update)์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ์— ํ•œ์ •ํ•ด let์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
(์žฌํ• ๋‹น(update)์€ ํ˜„์žฌ ๋ณ€์ˆ˜์— ์ €์žฅ๋œ ๊ฐ’์„ ๋ฒ„๋ฆฌ๊ณ  ์ƒˆ๋กœ์šด ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด๋‹ค.)


for/In ๋ฃจํ”„ p.310 

ํ˜•์‹: for (๋ณ€์ˆ˜ in ๊ฐ์ฒด)
{             
                  ๋ฌธ์žฅ;
}
ํŠน์ง•: ๊ฐ์ฒด(object) ์•ˆ์˜ ์†์„ฑ๋“ค์— ๋Œ€ํ•˜์—ฌ ์–ด๋–ค ์ฒ˜๋ฆฌ๋ฅผ ๋ฐ˜๋ณตํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ์ด๋‹ค. 

(๊ฐ์ฒด: ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š” ํ•˜๋‚˜์˜ ์—”ํ„ฐํ‹ฐ(๋ฌถ์Œ))

//Array ๊ฐ์ฒด๋กœ ๋ฐฐ์—ด ์ƒ์„ฑ 1
var myArray1 = new Array();
myArray1[0] = "apple";
myArray1[1] = "banana";
myArray1[2] = "orange";

//Array ๊ฐ์ฒด๋กœ ๋ฐฐ์—ด ์ƒ์„ฑ 2
var myArray2 = new Array("apple","banana","orange");

//๋ฆฌํ„ฐ๋Ÿด๋กœ ๋ฐฐ์—ด ์ƒ์„ฑ 
var myArray3 = ["apple", "banana", "orange"];


for(var i = 0; i<myArray1.length; i++){
document.write(myArray1[i]+" ");
}

for(var i = 0; i<myArray2.length; i++){
document.write(myArray2[i]+" ");
}

for(var i = 0; i<myArray3.length; i++){
document.write(myArray3[i]+" ");
}

Ch. 10 DOM๊ณผ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ, ์ž…๋ ฅ ๊ฒ€์ฆ 

์ฑ…์˜ ๋ชฉ์ฐจ
-๋ฌธ์„œ ๊ฐ์ฒด ๋ชจ๋ธ(DOM)
-HTML ์š”์†Œ ์ฐพ๊ธฐ
-HTML ๋ณ€๊ฒฝํ•˜๊ธฐ
-DOM ๋…ธ๋“œ ์‚ญ์ œ์™€ ์ถ”๊ฐ€
-๋ธŒ๋ผ์šฐ์ € ๊ฐ์ฒด ๋ชจ๋ธ
-์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ
-ํผ์˜ ์œ ํšจ์„ฑ ๊ฒ€์ฆ

 

๋ชฉํ‘œ
- HTML ๋ฌธ์„œ๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ DOM(Document Object Model)๋กœ ํ‘œํ˜„๋œ๋‹ค๋Š” ๊ฒƒ์„ ์ดํ•ดํ•œ๋‹ค.
- ์›น ๋ธŒ๋ผ์šฐ์ €๋„ BOM(Browser Object Model)๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์ดํ•ดํ•œ๋‹ค. 
- ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ๋ฅผ ์ดํ•ดํ•˜๊ณ  ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.
- ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•œ ์ž…๋ ฅ ๊ฒ€์ฆ์„ ํ•™์Šตํ•œ๋‹ค.

๋‚ด๊ฐ€ ์›ํ•˜๋Š” ์„ ํƒ์ž๋ฅผ ์ฐพ์„ ๋•Œ ๋„์›€์ด ๋œ๋‹ค.


1. ๋ฌธ์„œ ๊ฐ์ฒด ๋ชจ๋ธ(DOM)

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•ด ์–ด๋–ป๊ฒŒ HTML ๋ฌธ์„œ์˜ ์š”์†Œ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ํ•™์Šตํ•œ๋‹ค. 
๋˜ ์š”์†Œ๋กœ๋ถ€ํ„ฐ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ํ•™์Šตํ•œ๋‹ค. 

 

1.1 DOM๊ณผ BOM

DOM(Document Object Model): HTML ๋ฌธ์„œ๋ฅผ ๊ฐ์ฒด๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ
BOM(Browser Object Model): ์›น๋ธŒ๋ผ์šฐ์ €๋ฅผ ๊ฐ์ฒด๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ

1.2 ๋…ธ๋“œ์˜ ์ข…๋ฅ˜


2. HTML ์š”์†Œ ์ฐพ๊ธฐ

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•ด HTML ์š”์†Œ(element)๋ฅผ ์กฐ์ž‘ํ•˜์—ฌ ๋™์ ์ธ ์›นํŽ˜์ด์ง€๋ฅผ ์ž‘์„ฑํ•˜๋ ค๋ฉด ๋จผ์ € ์›ํ•˜๋Š” ์š”์†Œ๋ฅผ ์ฐพ์•„์•ผ ํ•œ๋‹ค.

1) id๋กœ HTML ์š”์†Œ ์ฐพ๊ธฐ p. 378

DOM์—์„œ HTML ์š”์†Œ๋ฅผ ์ฐพ๋Š” ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์€ ์š”์†Œ์˜ id๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
id ์„ ํƒ์ž๋Š” ๋ณ€์ˆ˜์— ์ €์žฅ์ด ๋œ๋‹ค.

์˜ˆ์‹œ) ๋ฌธ์„œ ์•ˆ์—์„œ id๊ฐ€ "main"์ธ ์š”์†Œ๋ฅผ ์ฐพ์•„์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

var x = document.getElementById("main");

๋งŒ์•ฝ ์š”์†Œ๊ฐ€ ๋ฐœ๊ฒฌ๋˜๋ฉด getElementById()๋Š” ์š”์†Œ๋ฅผ ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์š”์†Œ๊ฐ€ ๋ฐœ๊ฒฌ๋˜์ง€ ์•Š์œผ๋ฉด x๋Š” null์ด ๋œ๋‹ค.
์ฃผ์˜ํ•  ์ ์€  getElementById()๋Š” ์š”์†Œ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋‚ด์šฉ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์š”์†Œ ์ž์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. 
๋”ฐ๋ผ์„œ ์š”์†Œ์˜ ๋‚ด์šฉ์„ ์ถ”์ถœํ•˜๋ ค๋ฉด ์†์„ฑ innerHTML์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
innerHTML์€ ์š”์†Œ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ฝ˜ํ…์ธ ๋ฅผ ๊ฐ€์ ธ์˜ค๋ฏ€๋กœ innerHTML์„ ์ด์šฉํ•ด <div id = "main">์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ฝ˜ํ…์ธ ๋ฅผ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

alert(document.getElementById("main").innerHTML);

 

์˜ˆ์‹œ)

<script>
function process(){
// 1. id๊ฐ’์œผ๋กœ html ์š”์†Œ ์ฐพ๊ธฐ 
var obj = document.getElementById("target");
alert(obj.value);
}
</script>
</head>

<body>
<form name="myform">
<input type="text" id="target" name="text1">
<input type="submit" value="์ œ์ถœ" onclick="process()">
</form>
</body>

๊ฒฐ๊ณผ)

 


2) ์ž…๋ ฅ ์–‘์‹ ์ฐพ๊ธฐ

๋ฐ˜๋“œ์‹œ <form> ํƒœ๊ทธ๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค. 

์˜ˆ์‹œ)

<script>
function process(){
// 2. ์ž…๋ ฅ์–‘์‹ ์ฐพ๊ธฐ
var obj = document.myform.text1;
alert(obj.value);
}
</script>
</head>

<body>
<form name="myform">
<input type="text" id="target" name="text1">
<input type="submit" value="์ œ์ถœ" onclick="process()">
</form>
</body>

๊ฒฐ๊ณผ)


3) ํƒœ๊ทธ ์ด๋ฆ„์œผ๋กœ HTML ์š”์†Œ ์ฐพ๊ธฐ

getElementsByTagName() ๋ฉ”์„œ๋“œ๋Š” ํƒœ๊ทธ์˜ ์ด๋ฆ„์„ ์ธ์ˆ˜๋กœ ๋ฐ›์•„์„œ ์ด ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋ฐฐ์—ด์— ๋„ฃ์–ด์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 
์–ด๋–ค ๋…ธ๋“œ๊ฐ€ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉด, ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋Š” ์ฒซ ๋ฒˆ์งธ ์ž์‹ ๋…ธ๋“œ๋กœ ์ €์žฅ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์ฃผ์˜ํ•˜์ž. 

var eleArray = document.getElementsByTagName("div");

eleArray: ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 
div: ๋ฌธ์„œ ์•ˆ <div> ํƒœ๊ทธ๋ฅผ ๋ชจ๋‘ ์ฐพ์•„์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์˜ˆ์‹œ) ๋ฆฌ์ŠคํŠธ ํ•ญ๋ชฉ์„ ์ „๋ถ€ ์ถœ๋ ฅํ•œ๋‹ค.

<ul>
<li>List item1</li>
<li>List item2</li>
<li>List item3</li>
<li>List item4</li>
<li>List item5</li>
</ul>
<script>
//๋จผ์ € ๋ฌธ์„œ ๋‚ด์˜ ์ฒซ๋ฒˆ์งธ <ul> ์š”์†Œ๋ฅผ ์ฐพ๋Š”๋‹ค.
var list = document.getElementsByTagName('ul')[0];
var allItems = list.getElementsByTagName('li');
//for ๋ฐ˜๋ณต ๋ฃจํ”„๋ฅผ ์ด์šฉํ•ด ์ฒซ ๋ฒˆ์งธ ์ž์‹ ๋…ธ๋“œ์˜ data ํ•„๋“œ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
//์ฒซ ๋ฒˆ์งธ ์ž์‹ ๋…ธ๋“œ๋Š” ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋…ธ๋“œ์ด๋‹ค.
for (var i = 0; i < allItems.length; i++){
alert(allItems[i].firstChild.data);
}
</script>

๊ฒฐ๊ณผ

์˜ˆ์‹œ2)

<div>
<p>๊ณผ์ผ์˜ ๋ฆฌ์ŠคํŠธ</p>
<ul>
<li>์‚ฌ๊ณผ</li>
<li>๋ฐ”๋‚˜๋‚˜</li>
<li>์˜ค๋ Œ์ง€</li>
</ul>
</div>
<script>
var myDiv=document.getElementsByTagName("div")[0];
console.log(myDiv);
</script>

 

๊ฒฐ๊ณผ)


4) DOM ํŠธ๋ฆฌ ์ˆœํšŒ

์ถœ์ฒ˜:&nbsp;https://hianna.tistory.com/712



์ž์‹ ๋…ธ๋“œ์™€ ๋ถ€๋ชจ ๋…ธ๋“œ ๊ด€๊ณ„๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•œ ๋…ธ๋“œ์”ฉ ๋ฐฉ๋ฌธํ•œ๋‹ค. 
ex) childNodes[], nextSibling[], parentNode[]๋ฅผ ์ด์šฉํ•œ๋‹ค.

<div>
          <p> ๊ณผ์ผ ๋ฆฌ์ŠคํŠธ </p>
          <ul>
                    <li> ์‚ฌ๊ณผ </li>
                    <li> ๋ฐ”๋‚˜๋‚˜ </li>
                    <li> ์˜ค๋ Œ์ง€ </li>
          </ul>
</div>

1) childNodes: ํ•œ ์š”์†Œ์˜ ๋ชจ๋“  ์ž์‹ ์š”์†Œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐฐ์—ด์ด ๋ฐ˜ํ™˜๋œ๋‹ค.
ex) myUl.childNode[0];  = <li> ์‚ฌ๊ณผ </li>

2) firstChild: 'childNodes' ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค. 'childNodes[0]'๊ณผ ๊ฐ™๋‹ค. 
ex) var myP = myDiv.firstchild; = <p> ๊ณผ์ผ ๋ฆฌ์ŠคํŠธ </p>

3) lastChild: 'childNodes' ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค. 'childNodes[childNodes-1]'๊ณผ ๊ฐ™๋‹ค.

4) parentNode: ํ˜„์žฌ ๋…ธ๋“œ์˜ ๋ถ€๋ชจ ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

5) nextSibling: ํ˜„์žฌ ๋…ธ๋“œ์˜ ๋‹ค์Œ ํ˜•์ œ ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
ex) var myUl = myP.nextSibling; =

          <ul>
                    <li> ์‚ฌ๊ณผ </li>
                    <li> ๋ฐ”๋‚˜๋‚˜ </li>
                    <li> ์˜ค๋ Œ์ง€ </li>
          </ul>


6) previousSibling: ํ˜„์žฌ ๋…ธ๋“œ์˜ ์ด์ „ ํ˜•์ œ ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.


์˜ˆ์‹œ1)

<div>
<!-- ์ฒซ๋ฒˆ์งธ ์ž์‹ -->
<p>๊ณผ์ผ์˜ ๋ฆฌ์ŠคํŠธ</p>
<!-- ๋‘๋ฒˆ์งธ ์ž์‹ -->
<ul>
<li>์‚ฌ๊ณผ</li>
<li>๋ฐ”๋‚˜๋‚˜</li>
<li>์˜ค๋ Œ์ง€</li>
</ul>
</div>
<script>
var myP=myDiv.firstChild;
console.log(myP);
</script>

 

๊ฒฐ๊ณผ)

 

์˜ˆ์‹œ2)

<div>
<!-- ์ฒซ๋ฒˆ์งธ ์ž์‹ -->
<p>๊ณผ์ผ์˜ ๋ฆฌ์ŠคํŠธ</p>
<!-- ๋‘๋ฒˆ์งธ ์ž์‹ -->
<ul>
<li>์‚ฌ๊ณผ</li>
<li>๋ฐ”๋‚˜๋‚˜</li>
<li>์˜ค๋ Œ์ง€</li>
</ul>
</div>
<script>
var myDiv=document.getElementsByTagName("div")[0];
console.log(myDiv);
//<div>์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ์ด๋ฏ€๋กœ <p>์ด๋‹ค.
var myP=myDiv.firstChild;
console.log(myP);
//<div>๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋งˆ์ง€๋ง‰ ์š”์†Œ์ด๋ฏ€๋กœ <ul>์ด๋‹ค. 
var myL=myDiv.lastChild;
console.log(myL);
var myUI=myP.nextSibling;
console.log(myUI);
</script>

3 HTML ๋ณ€๊ฒฝํ•˜๊ธฐ

HTML ๋ฌธ์„œ๊ฐ€ ์ง์ ‘ ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๊ณ  DOM๋งŒ ๋ณ€๊ฒฝ๋œ๋‹ค.

3.1 ์š”์†Œ์˜ ๋‚ด์šฉ ๋ณ€๊ฒฝํ•˜๊ธฐ p. 384

ํŠน์ • HTML ์š”์†Œ์˜ ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์œผ๋ฉด innerHTML ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
innerHTML์€ ์š”์†Œ์˜ ์‹œ์ž‘ ํƒœ๊ทธ์™€ ์ข…๋ฃŒ ํƒœ๊ทธ ์‚ฌ์ด์— ๋†“์ธ ๋ชจ๋“  HTML ์ฝ”๋“œ์™€ ํ…์ŠคํŠธ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

<div id = "main"> ์ด๊ฒƒ์ด div ์š”์†Œ์ž…๋‹ˆ๋‹ค. </div>

<div id = "main">: ์‹œ์ž‘ ํƒœ๊ทธ
์ด๊ฒƒ์ด div ์š”์†Œ์ž…๋‹ˆ๋‹ค.: <div> ์š”์†Œ์˜ innerHTML
</div>: ์ข…๋ฃŒ ํƒœ๊ทธ

innerHTML ์†์„ฑ์€ (ํ…์ŠคํŠธ)๋งŒ ๊ฐ€์ง€๊ณ  ์žˆ์„ ์ˆ˜๋„ ์žˆ๊ณ , (HTML + ํ…์ŠคํŠธ)์ผ ์ˆ˜๋„ ์žˆ๋‹ค. 
ex) <strong> ํƒœ๊ทธ๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜๋„ ์žˆ๋‹ค.

์–ด๋–ค ์š”์†Œ์˜ ์ฝ˜ํ…์ธ ๋ฅผ ๋ณ€๊ฒฝํ•˜๋ผ๋ฉด (์ฆ‰ ์‹œ์ž‘ ํƒœ๊ทธ์™€ ์ข…๋ฃŒ ํƒœ๊ทธ ์‚ฌ์ด์˜ ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด)
innerHTML ์†์„ฑ์— ์›ํ•˜๋Š” ๋‚ด์šฉ์„ ๋Œ€์ž…ํ•ด ์ฃผ๋ฉด ๋œ๋‹ค.

id = "main"์ธ <div> ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค. HTML DOM์„ ์ด์šฉํ•ด id = "main"์ธ ์š”์†Œ๋ฅผ ์ฐพ๋Š”๋‹ค.
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์š”์†Œ์˜ ๋‚ด์šฉ(innerHTML)์„ ๋ณ€๊ฒฝํ•œ๋‹ค.

document.getElementById("main).innerHTML = "์›น ํŽ˜์ด์ง€๊ฐ€ ์ž‘์†ก๋˜์—ˆ์Šต๋‹ˆ๋‹ค.";

 

์˜ˆ์‹œ 1) 
๋กœ๊ทธ์ธ → ๋กœ๊ทธ์•„์›ƒ๊ณผ ๊ฐ™์ด ์›๋ฆฌ์ด๋‹ค.

<script>
function get(){
var val = document.getElementById("ex").innerHTML;
alert(val);
}
function set(v){
document.getElementById("ex").innerHTML = v;
}
</script>
</head>

<body>
<div id = "ex">์—ฌ๊ธฐ๊ฐ€ div๋กœ ์„ ์–ธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.</div>
<a href = "#" onclick="get()">div ์š”์†Œ ๋‚ด์šฉ ์ถœ๋ ฅํ•˜๊ธฐ</a><br>
<a href = "#" onclick="set('๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.')">div ์š”์†Œ ๋‚ด์šฉ ๋ณ€๊ฒฝํ•˜๊ธฐ</a>
</body>

๊ฒฐ๊ณผ)

 


์˜ˆ์‹œ 2) ๋กœ๊ทธ์ธ์—์„œ ๋กœ๊ทธ์•„์›ƒ ์ƒํƒœ๋กœ ๋ฐ”๊พธ๊ธฐ

<a href = "#" onclick = "logout()" id = "log">๋กœ๊ทธ์ธ</a>
๋กœ๊ทธ์ธ์€ ์œ„์˜ innerHTML ์˜์—ญ์ด๋‹ค. input ํƒœ๊ทธ์ฒ˜๋Ÿผ ๋‹ซ๋Š” ํƒœ๊ทธ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ๋ชป์“ด๋‹ค.

<script>
  function logout(){
  document.getElementById("log").innerHTML = "๋กœ๊ทธ์•„์›ƒ";
  }
</script>
</head>
<body>
์•„์ด๋”” : <input type="text">
๋น„๋ฐ€๋ฒˆํ˜ธ : <input type="password">
<a href = "#" onclick = "logout()" id = "log">๋กœ๊ทธ์ธ</a>

 

์˜ˆ์‹œ 2.1) ๋กœ๊ทธ์ธ์—์„œ ๋กœ๊ทธ์•„์›ƒ ์ƒํƒœ๋กœ ๋ฐ”๊พธ๊ธฐ 

<script>
function set(logoff){
document.getElementById("current").innerHTML = logoff;
}
</script>
</head>
<body>
<div id = "current">๋กœ๊ทธ์ธ ์ƒํƒœ์ž…๋‹ˆ๋‹ค.</div>
<a href = "#" onclick="set('๋กœ๊ทธ์•„์›ƒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.')">๋กœ๊ทธ์•„์›ƒ</a>
</body>

๊ฒฐ๊ณผ)

 


์˜ˆ์‹œ 3) ๋กœ๊ทธ์ธ์—์„œ ๋กœ๊ทธ์•„์›ƒ ๋ฒ„ํŠผ ๋ˆŒ๋Ÿฌ์„œ ๋กœ๊ทธ์•„์›ƒ ์ƒํƒœ๋กœ ๋ฐ”๊พธ๊ธฐ

<script>
function set(logoff){
document.getElementById("current").innerHTML = logoff;
}
</script>
</head>
<body>
<div id = "current">๋กœ๊ทธ์ธ ์ƒํƒœ์ž…๋‹ˆ๋‹ค.</div>
<input type = "button" value ="๋กœ๊ทธ์•„์›ƒ" onclick="set('๋กœ๊ทธ์•„์›ƒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.')">
</body>

๊ฒฐ๊ณผ)

์˜ˆ์‹œ 4)

<div id = "current">
์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.<br>
<input type = "text" placeholder ="ID">
<input type = "text" placeholder ="PASSWORD">
<input type = "button" value ="๋กœ๊ทธ์ธ" onclick="set('๋กœ๊ทธ์ธ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.')">
<input type = "button" value ="๋กœ๊ทธ์•„์›ƒ" onclick="set('๋กœ๊ทธ์•„์›ƒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.')">
</div>

๊ฒฐ๊ณผ)

 


์˜ˆ์‹œ 5)

์•„์ด๋””์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ž€์„ ๋งŒ๋“ค๊ณ  ์•„์ด๋””์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋งž์œผ๋ฉด
๋กœ๊ทธ์ธ -> ๋กœ๊ทธ์•„์›ƒ์œผ๋กœ ๋ณ€๊ฒฝํ•˜๊ณ 
๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด "์•„์ด๋”” ๋ฐ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค"๋ผ๋Š” ์•Œ๋ฆผ์ฐฝ์„ ๋„์šฐ์„ธ์š”.
 id: abcd, password: 1234๋กœ ๊ฐ€์ •ํ•œ๋‹ค. (DB์— ์ €์žฅ๋˜์–ด ์žˆ๋‹ค.)

๋‚ด๊ฐ€ ํ—ท๊ฐˆ๋ ธ๋˜ ๋ถ€๋ถ„์€ <input type="button">์— id๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.
btnElement = document.getElementById('btn');
      btnElement.value = "๋กœ๊ทธ์•„์›ƒ";
๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฒ„ํŠผ์ด ๋กœ๊ทธ์ธ์—์„œ ๋กœ๊ทธ์•„์›ƒ์œผ๋กœ ๋ฐ”๋€๋‹ค. 

๊ทธ๋ฆฌ๊ณ  input์— ๋„ฃ์€ ๊ฐ value ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด
  var userid = document.getElementById('userid').value;
  var userpassword = document.getElementById('userpassword').value;
๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 

์ฝ”๋“œ 5) ๋‚ด๊ฐ€ ์“ด ์ฝ”๋“œ 

<script>
function test() {//starts
  var userid = document.getElementById('userid').value;
  var userpassword = document.getElementById('userpassword').value;

  if( userid !== "abcd" || userpassword != 1234 ) {
       alert("์•„์ด๋”” ๋˜๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค");
       return false;
      }
  else {
  const btnElement = document.getElementById('btn');
      btnElement.value = "๋กœ๊ทธ์•„์›ƒ";
  }
}//ends
</script>
</head>
<body>
<div>
<form name="join">
์•„์ด๋”” : <input type="text" id="userid">
๋น„๋ฐ€๋ฒˆํ˜ธ : <input type="password" id="userpassword">
<input type="button" id='btn' onclick="test()" value="๋กœ๊ทธ์ธ"> 
</form>
</div>
</body>

์ฝ”๋“œ 5.1) ์ˆ˜์—…์˜ ์ฝ”๋“œ

<script> 
 //input ํƒœ๊ทธ์˜ value ๊ฐ’์ด ํ•„์š”ํ•˜๋‹ค. 
  function logout(){
  var id = document.getElementById("id").value;
  var password = document.getElementById("password").value;
  //์•„์ด๋””๊ฐ€ "log"์ธ ๊ฒƒ์„ ์„ ํƒํ•œ๋‹ค. 
  if (id == "abcd" && password == "1234"){
  document.getElementById("log").innerHTML = "๋กœ๊ทธ์•„์›ƒ";
  }
  else {
  alert("์•„์ด๋”” ๋ฐ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค");
  }
  }
</script>
</head>
<body>
์•„์ด๋”” : <input type="text">
๋น„๋ฐ€๋ฒˆํ˜ธ : <input type="password">
<!-- ๋กœ๊ทธ์ธ์€ ์œ„์˜ innerHTML ์˜์—ญ์ด๋‹ค. 
์œ„์˜ input ํƒœ๊ทธ์ฒ˜๋Ÿผ ๋‹ซ๋Š” ํƒœ๊ทธ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ๋ชป์“ด๋‹ค.-->
<a href = "#" onclick = "logout()" id = "log">๋กœ๊ทธ์ธ</a>
</body>

๊ฒฐ๊ณผ 5) ์•„์ด๋”” ๋˜๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž˜๋ชป์ž…๋ ฅํ–ˆ์„ ๋•Œ

๊ฒฐ๊ณผ 5.2) ์•„์ด๋”” ๋ฐ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ •ํ™•ํžˆ ์ž…๋ ฅํ–ˆ์„ ๋•Œ ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์ด ๋กœ๊ทธ์•„์›ƒ์œผ๋กœ ๋ฐ”๋€Œ์—ˆ๋‹ค.


3.2 ์š”์†Œ์˜ ์†์„ฑ ๋ณ€๊ฒฝํ•˜๊ธฐ

์š”์†Œ๊ฐ€ ๊ฐ€์ง„ ์†์„ฑ์„ ๋™์ ์œผ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.
id๊ฐ€ "image"์ธ ์š”์†Œ๋ฅผ ์ฐพ์•„์„œ <img> ์š”์†Œ์˜ src ์†์„ฑ์„ ๋ณ€๊ฒฝํ•œ๋‹ค. 

document.getElementById("image").src = "poodle.png";

src: ์†์„ฑ ์ด๋ฆ„
"poodle.png": ์ƒˆ๋กœ์šด ์†์„ฑ๊ฐ’

์˜ˆ์‹œ)

<script>
function changeImage(){
document.getElementById("image").src="image/coffee.jpg";
}
</script>
</head>
<body>
<!-- ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์œ„์˜ image/coffee.jpg๋กœ ๋ฐ”๋€๋‹ค. -->
<img id="image" src = "image/coffee.webp" width="120" height="100"><br>
<input type = "button" value="๋ˆŒ๋Ÿฌ๋ณด์„ธ์š”." onclick="changeImage()">

๊ฒฐ๊ณผ)


3.3 ์š”์†Œ์˜ ์Šคํƒ€์ผ ๋ณ€๊ฒฝํ•˜๊ธฐ

DOM๊ณผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด HTML ์š”์†Œ์˜ ์Šคํƒ€์ผ๋„ ์–ผ๋งˆ๋“ ์ง€ ๋™์ ์œผ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

document.getElementById("p2").style.color = "blue";

style: ๋ฐ˜๋“œ์‹œ style์„ ๋ถ™์ธ๋‹ค.
color: ์†์„ฑ ์ด๋ฆ„
"blue": ์ƒˆ๋กœ์šด ์†์„ฑ๊ฐ’ 

์˜ˆ์‹œ1)

<script>
function changeStyle(){
document.getElementById("p1").style.color="red";
document.getElementById("p1").style.fontFamily="๊ถ์„œ์ฒด";
document.getElementById("p1").style.fontStyle="italic";
}
</script>
</head>
<body>
<p id="p1">This is a paragraph.</p>
<input type = "button" onclick = "changeStyle()" value = "๋ˆŒ๋Ÿฌ๋ณด์„ธ์š”.">
</body>

๊ฒฐ๊ณผ)

 

 

์˜ˆ์‹œ2) ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์š”์†Œ๋ฅผ ํ™”๋ฉด์—์„œ ์‚ฌ๋ผ์ง€๊ฒŒ ํ•œ๋‹ค. 

<h3 id = "heading">์ด๊ฒƒ์€ ํ—ค๋”ฉ์ž…๋‹ˆ๋‹ค.</h3>
<input type="button" value="์š”์†Œ์ˆจ๊น€" onclick="document.getElementById('heading').style.visibility = 'hidden'">
<input type="button" value="์š”์†Œํ‘œ์‹œ" onclick="document.getElementById('heading').style.visibility = 'visible'">

๊ฒฐ๊ณผ)


728x90
๋ฐ˜์‘ํ˜•

'๐ŸŒณBootcamp Revision 2023โœจ > JavaScript, jQuery, Ajax, JSON' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[18] 230120 Ch. 10 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ DOM๊ณผ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ, ์ž…๋ ฅ ๊ฒ€์ฆ: 7. ํผ์˜ ์œ ํšจ์„ฑ ๊ฒ€์ฆ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 18์ผ]  (0) 2023.01.20
[17] 230119 Ch. 10 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ DOM๊ณผ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ, ์ž…๋ ฅ ๊ฒ€์ฆ: 4. DOM ๋…ธ๋“œ ์‚ญ์ œ์™€ ์ถ”๊ฐ€, 5. ๋ธŒ๋ผ์šฐ์ € ๊ฐ์ฒด ๋ชจ๋ธ, 6. ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 17์ผ]  (0) 2023.01.19
[15] 230117 Ch. 9 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด: 1. ๊ฐ์ฒด, 2. ๊ฐ์ฒด์˜ ์ƒ์„ฑ ๋ฐ ์‚ฌ์šฉ, 3. ํ”„๋กœํ† ํƒ€์ž…, 4. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‚ด์žฅ ๊ฐ์ฒด, 5. Array ๊ฐ์ฒด, 6. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ์˜ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 15์ผ]  (0) 2023.01.17
[14] 230116 Ch. 8 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ธฐ์ดˆ: 12. ํ•จ์ˆ˜, 13. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์ž…์ถœ๋ ฅ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 14์ผ]  (0) 2023.01.16
Comments