๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IT/HTML

[34์ผ์ฐจ] 8์žฅ HTML DOM๊ณผ Document

by GWLEE 2022. 8. 5.

๐Ÿ’›2022-08-05๐Ÿ’›

ex8-01.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>HTML DOM ํŠธ๋ฆฌ</title>
</head>
<body>
<h3>DOM ๊ฐ์ฒด p์˜ ํ”„๋กœํผํ‹ฐ, ์Šคํƒ€์ผ, ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ</h3>
<hr>
<p id="firstP" style="color:blue; background:yellow"
	onclick="this.style.color='teal'">์ด๊ฒƒ์€
	<span style="color:red">๋ฌธ์žฅ์ž…๋‹ˆ๋‹ค.</span>
</p>
<script>
	let p = document.querySelector("#firstP"); // ํ•˜๋‚˜ ๊ฐ€์ ธ์˜ค๋Š” ๊ฑฐ
	// let p = document.querySelectorAll() // ๋ชจ์กฐ๋ฆฌ ๋‹ค ๊ฐ€์ ธ์˜ค๋Š” ๊ฑฐ
	// let p = document.getElementById("firstP");
	let text = "p.id = " + p.id + "\n";
	text += "p.tagName = " + p.tagName + "\n";
	text += "p.innerHTML = " + p.innerHTML + "\n";
	text += "p.style.color = " + p.style.color + "\n";
	text += "p.onclick = " + p.onclick + "\n";
	text += "p.childElementCount = " + p.childElementCount + "\n";
	text += "๋„ˆ๋น„ = " + p.offsetWidth + "\n";
	text += "๋†’์ด = " + p.offsetHeight + "\n";
	alert(text);
</script>
</body>
</html>

 

ex8-02.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CSS ์Šคํƒ€์ผ ๋™์  ๋ณ€๊ฒฝ</title>
<script>
function change() {
	let span = document.getElementById("mySpan"); // id๊ฐ€ mySpan์ธ ๊ฐ์ฒด ์ฐพ๊ธฐ
	span.style.color = "green"; // ๊ธ€์ž ์ƒ‰ green
	span.style.fontSize = "30px"; // ๊ธ€์ž ํฌ๊ธฐ๋Š” 30ํ”ฝ์…€
	span.style.display = "block"; // ๋ธ”๋ก ๋ฐ•์Šค๋กœ ๋ณ€๊ฒฝ
	span.style.width = "6em"; // ๋ฐ•์Šค์˜ ํญ. 6 ๊ธ€์ž ํฌ๊ธฐ
	span.style.border = "3px dotted magenta"; // 3ํ”ฝ์…€ ์ ์„  magenta ํ…Œ๋‘๋ฆฌ
	span.style.margin = "20px"; // ์ƒํ•˜์ขŒ์šฐ ์—ฌ๋ฐฑ 20px
}
</script>
</head>
<body>
<h3>CSS ์Šคํƒ€์ผ ๋™์  ๋ณ€๊ฒฝ</h3>
<hr>
<p style="color:blue" >์ด๊ฒƒ์€
    <span id="mySpan" style="color:red">๋ฌธ์žฅ์ž…๋‹ˆ๋‹ค.	</span>
</p>
<input type="button" value="์Šคํƒ€์ผ๋ณ€๊ฒฝ" onclick="change()">
</body>
</html>

 

ex8-03.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>innerHTML ํ™œ์šฉ</title>
<script>
function change() {
	let p = document.getElementById("firstP");
	p.innerHTML= "๋‚˜์˜ <img src='media/puppy.png'> ๊ฐ•์•„์ง€";
}
</script>
</head>
<body>
<h3>innerHTML ํ™œ์šฉ : ์•„๋ž˜ ๊ธ€์ž์— ํด๋ฆญํ•˜๋ฉด ์˜ˆ์œ ๊ฐ•์•„์ง€๊ฐ€ ๋ณด์ž…๋‹ˆ๋‹ค.</h3>
<hr>
<p id="firstP" style="color:blue"
	onclick="change()">์—ฌ๊ธฐ์—
	<span style="color:red">ํด๋ฆญํ•˜์„ธ์š”</span>
</p>
</body>
</html>

 

ex8-04.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>this ํ™œ์šฉ</title>
<script>
function change(obj, size, color) {
	obj.style.color = color;
	obj.style.fontSize = size;
}
</script>
</head>
<body>
<h3>this ํ™œ์šฉ</h3>
<hr>
<button onclick="change(this, '30px', 'red')">๋ฒ„ํŠผ</button>
<button onclick="change(this, '30px', 'blue')">๋ฒ„ํŠผ</button>
<div onclick="change(this, '25px', 'orange')">
   ์—ฌ๊ธฐ ํด๋ฆญํ•˜๋ฉด ํฌ๊ธฐ์™€ ์ƒ‰ ๋ณ€๊ฒฝ
</div>
</body>
</html>

 

ex8-05.html

<!DOCTYPE html>
<html>
<head id="myHead">
<meta charset="UTF-8">
<title>document ๊ฐ์ฒด์˜ ์ฃผ์š” ํ”„๋กœํผํ‹ฐ</title>
<script>
	let text = "๋ฌธ์„œ ๋กœ๋”ฉ ์ค‘์ผ ๋•Œ readyState = " + document.readyState + "\n";
</script>
</head>
<body style="background-color:yellow; color:blue; direction:rtl"
      onload="printProperties()">
<h3>document์˜ ์ฃผ์š” ํ”„๋กœํผํ‹ฐ</h3>
<hr>
<a href="http://www.naver.com">๋„ค์ด๋ฒ„ ํ™ˆํŽ˜์ด์ง€</a>
<div>์ด๊ณณ์€ div ์˜์—ญ์ž…๋‹ˆ๋‹ค.</div>
<input id="input" type="text" value="์—ฌ๊ธฐ ํฌ์ปค์Šค๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค">

<script>
// ๋ฌธ์„œ๊ฐ€ ์™„์ „ํžˆ ๋กœ๋“œ(์ถœ๋ ฅ)๋˜์—ˆ์„ ๋•Œ, ํ˜„์žฌ document์˜ ํ”„๋กœํผํ‹ฐ ์ถœ๋ ฅ
function printProperties() {
	document.getElementById("input").focus(); // <input> ํƒœ๊ทธ์— ํฌ์ปค์Šค๋ฅผ ์คŒ
	
	text += "1. location =" + document.location + "\n";
	text += "2. URL =" + document.URL + "\n";
	text += "3. title =" + document.title + "\n";
	text += "4. head์˜ id =" + document.head.id + "\n";
	text += "5. body color =" + document.body.style.color + "\n";
	text += "6. domain =" + document.domain + "\n";;
	text += "7. lastModified =" + document.lastModified + "\n";
	text += "8. defaultView = " + document.defaultView + "\n";
	text += "9. ๋ฌธ์„œ์˜ ๋กœ๋“œ ์™„๋ฃŒ ํ›„ readyState = " + document.readyState + "\n";
	text += "10. referrer = " + document.referrer + "\n";
	text += "11. activeElement = " + document.activeElement.value + "\n";
	text += "12. documentElement์˜ ํƒœ๊ทธ ์ด๋ฆ„ = " + document.documentElement.tagName + "\n";
	alert(text);
}
</script>

</body>
</html>

 

ex8-06.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>document.getElementsByTagName()</title>
<script>
function change() {
	let spanArray = document.querySelectorAll("span");

	// for(let i=0; i<spanArray.length; i++) {
	// 	let span = spanArray[i];
	// 	span.style.color = "orchid";
	// 	span.style.fontSize = "20px";
	// }

	for(let span of spanArray) {
		span.style.color = "orchid";
		span.style.fontSize = "20px";
	}
}

</script>
</head>
<body>
<h3>๋‚ด๊ฐ€ ์ข‹์•„ํ•˜๋Š” ๊ณผ์ผ
	<button onclick="change()">๋ˆ„๋ฅด์„ธ์š”</button>
</h3>
<hr>
์ €๋Š” ๋นจ๊ฐ„ <span>์‚ฌ๊ณผ</span>๋ฅผ ์ข‹์•„ํ•ด์„œ
์•„์นจ๋งˆ๋‹ค ํ•œ ๊ฐœ์”ฉ ๋จน๊ณ  ์žˆ์–ด์š”. ์šด๋™ํ•  ๋•Œ๋Š” ์ค‘๊ฐ„ ์ค‘๊ฐ„์—
<span>๋ฐ”๋‚˜๋‚˜</span>๋ฅผ ๋จน์ง€์š”. ํƒ„์ˆ˜ํ™”๋ฌผ ์„ญ์ทจ๊ฐ€ ๋นจ๋ผ
ํž˜์ด ๋‚ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋‹ฌ์ฝคํ•œ ํ–ฅ๊ธฐ๋ฅผ ํ’ˆ์€ <span>์ฒด๋ฆฌ</span>์™€
์—ฌ๋ฆ„ ๋ƒ„์ƒˆ ๋ฌผ์”ฌ ๋‚˜๋Š” <span>์ž๋‘</span>๋ฅผ ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค.
</body>
</html>

 

ex8-07.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>document.getElementsByClassName()</title>
<script>
function viewPlace() {
	let tagArray = document.querySelectorAll(".place");
 	for(let tag of tagArray) {
		tag.style.color = "orchid";
		tag.style.fontSize = "20px";
		tag.style.textDecoration = "underline";
 	}
}

function viewFood() {
	let tagArray = document.querySelectorAll(".food");
 	for(let tag of tagArray) {
		tag.style.color = "darkcyan";
 	}
}
</script>
</head>
<body>
<h3>๊ฐ€๊ณ  ์‹ถ์€ ๊ณณ ๋จน๊ณ  ์‹ถ์€ ๊ฒƒ</h3><br>
	<button onclick="viewPlace()">๊ฐ€๊ณ  ์‹ถ์€ ๊ณณ</button>
	<button onclick="viewFood()">๋จน๊ณ  ์‹ถ์€ ๊ฒƒ</button>
</h3>
<hr>
<p><span class="place">์ œ์ฃผ๋„</span>์— ๊ฐ€์„œ <span class="food">ํ‘๋ผ์ง€</span>๋ฅผ ๋จน๊ณ  ์‹ถ๊ณ ์š”.
<span class="place">๋…๋„</span>์— ๊ฐ€์„œ <span class="food">๋…๋„ ์ƒˆ์šฐ</span>๋„ ๋จน๊ณ  ์‹ถ์–ด์š”. ์ œ์ผ ๊ฐ€๊ณ  ์‹ถ์€ ๊ณณ <span class="place">๋ถ€์‚ฐ ์ž๊ฐˆ์น˜ ์‹œ์žฅ</span>์—์„œ <span class="food">๊ผผ์žฅ์–ด ๊ตฌ์ด</span>๋„ ๋จน๊ณ  ์‹ถ์–ด์š”</p>
</body>
</html>

 

ex8-08.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>write()์™€ writeln()</title></head>
<body>
<h3>write()์™€ writeln()</h3>
<hr>
<script>
	document.write("<h3>๋™๋ฌผ์›์— ์†Œํ’๊ฐ‘์‹œ๋‹ค</h3>");
	document.write("<p style='color:blue'>๋‚ ์”จ๊ฐ€ ์ข‹์•„ ");
	document.write("์†Œํ’๊ฐ‘๋‹ˆ๋‹ค</p>");
	document.write(2+3);
	document.write("๋ช…์ž…๋‹ˆ๋‹ค.<br>"); // ๋‹ค์Œ ์ค„๋กœ ๋„˜์–ด๊ฐ€๊ธฐ

	document.writeln(5); // ๋‹ค์Œ ์ค„์— ๋„˜์–ด๊ฐ€์ง€ ๋ชปํ•จ
	document.writeln("๋ช…์ž…๋‹ˆ๋‹ค.<br>");
</script>
</body>
</html>

 

ex8-09.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>write()๋ฅผ ์ž˜๋ชป ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ</title>
</head>
<body onclick="document.write('<h3>ํด๋ฆญ๋˜์—ˆ์Šต๋‹ˆ๋‹ค</h3>')">
<h3>write()๋ฅผ ์ž˜๋ชป ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ</h3>
<hr>
<p>์›น๋ธŒ๋ผ์šฐ์ €์˜ ๋ฐ”ํƒ• ์•„๋ฌด ๊ณณ์ด๋‚˜
     ํด๋ฆญํ•ด๋ณด์„ธ์š”.</p>
</body>
</html>

 

ex8-10.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>HTML ๋ฌธ์„œ ์ž‘์„ฑ๊ธฐ ๋งŒ๋“ค๊ธฐ</title>
<script>
let win=null;
function showHTML() {
	if(win == null || win.closed)
		win = window.open("", "outWin", "width=250,height=150");

	let textArea = document.getElementById("srcText");
	win.document.open();
	win.document.write(textArea.value);
	win.document.close();
}
</script>
</head>
<body>
<h3>HTML ๋ฌธ์„œ ์ž‘์„ฑ๊ธฐ ๋งŒ๋“ค๊ธฐ </h3>
<hr>
<p>์•„๋ž˜์— HTML ๋ฌธ์„œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ด ๋ณด์„ธ์š”.
์ƒˆ ์œˆ๋„์šฐ์— HTML ๋ฌธ์„œ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.</p>
<textarea id="srcText" rows="10" cols="40"></textarea>
<br>
<br>
<button onclick="showHTML()">HTML ๋ฌธ์„œ ์ถœ๋ ฅํ•˜๊ธฐ</button>
</body>
</html>

 

ex8-11.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๋ฌธ์„œ์˜ ๋™์  ๊ตฌ์„ฑ</title>
<script>
function createDIV() {
	let obj = document.querySelector("#parent");
	let newDIV = document.createElement("div");
	newDIV.innerHTML = "์ƒˆ๋กœ ์ƒ์„ฑ๋œ DIV์ž…๋‹ˆ๋‹ค.";
	newDIV.setAttribute("id", "myDiv");
	newDIV.style.backgroundColor = "yellow";
	newDIV.onclick = function() {
		let p = this.parentElement; // ๋ถ€๋ชจ HTML ํƒœ๊ทธ ์š”์†Œ
		p.removeChild(this); // ์ž์‹ ์„ ๋ถ€๋ชจ๋กœ๋ถ€ํ„ฐ ์ œ๊ฑฐ
	};
	obj.appendChild(newDIV);
}
</script>
</head>
<body id="parent">
<h3>DIV ๊ฐ์ฒด๋ฅผ ๋™์ ์œผ๋กœ ์ƒ์„ฑ, ์‚ฝ์ž…, ์‚ญ์ œํ•˜๋Š” ์˜ˆ์ œ</h3>
<hr>
<p>DOM ํŠธ๋ฆฌ์— ๋™์ ์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
createElement(), appendChild(),
removeChild() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ,
์‚ฝ์ž…, ์‚ญ์ œํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.</p>
<a href="javascript:createDIV()">DIV ์ƒ์„ฑ</a><p>
<p>
</body>
</html>

๋Œ“๊ธ€