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

[34์ผ์ฐจ] 9์žฅ ์ด๋ฒคํŠธ ๊ธฐ์ดˆ ๋ฐ ํ™œ์šฉ

by GWLEE 2022. 8. 5.

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

 

ex9-01.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>HTML ํƒœ๊ทธ์— ๋ฆฌ์Šค๋„ˆ ์ž‘์„ฑ</title>
</head>
<body>
<h3>HTML ํƒœ๊ทธ์— ๋ฆฌ์Šค๋„ˆ ์ž‘์„ฑ</h3>
<hr>
<p onmouseover="this.style.backgroundColor='orchid'"
   onmouseout="this.style.backgroundColor='white'">
๋งˆ์šฐ์Šค ์˜ฌ๋ฆฌ๋ฉด orchid ์ƒ‰์œผ๋กœ ๋ณ€๊ฒฝ</p>
</body>
</html>

 

ex9-02.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>DOM ๊ฐ์ฒด์˜ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ํ”„๋กœํผํ‹ฐ์— ํ•จ์ˆ˜ ๋“ฑ๋ก</title>
<script>
let p;
function init() { // ๋ฌธ์„œ๊ฐ€ ์™„์ „ํžˆ ๋กœ๋“œ๋˜์—ˆ์„ ๋•Œ ํ˜ธ์ถœ
	p = document.getElementById("p");
	p.onmouseover = over; // over() ํ•จ์ˆ˜๋ฅผ onmouseover ๋ฆฌ์Šค๋„ˆ๋กœ ๋“ฑ๋ก
	p.onmouseout = out; // out() ํ•จ์ˆ˜๋ฅผ onmouseout ๋ฆฌ์Šค๋„ˆ๋กœ ๋“ฑ๋ก
}
function over() {
	p.style.backgroundColor="orchid";
}
function out() {
	p.style.backgroundColor="white";
}
</script></head>
<body onload="init()">
<h3>DOM ๊ฐ์ฒด์˜ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ํ”„๋กœํผํ‹ฐ์— ํ•จ์ˆ˜ ๋“ฑ๋ก</h3>
<hr>
<p id="p">๋งˆ์šฐ์Šค ์˜ฌ๋ฆฌ๋ฉด orchid ์ƒ‰์œผ๋กœ ๋ณ€๊ฒฝ</p>
</body>
</html>

 

ex9-03.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>addEventListener()๋ฅผ ์ด์šฉํ•œ ๋ฆฌ์Šค๋„ˆ ๋“ฑ๋ก</title>
<script>
let p;
function init() { // ๋ฌธ์„œ๊ฐ€ ์™„์ „ํžˆ ๋กœ๋“œ๋˜์—ˆ์„ ๋•Œ ํ˜ธ์ถœ
	p = document.getElementById("p");
	p.addEventListener("mouseover", over); // ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ๋“ฑ๋ก
	p.addEventListener("mouseout", out); // ์ด๋ฒคํŠธ  ๋ฆฌ์Šค๋„ˆ ๋“ฑ๋ก
}
function over() {
	p.style.backgroundColor="orchid";
}
function out() {
	p.style.backgroundColor="white";
}
</script>
</head>
<body onload="init()">
<h3>addEventListener()๋ฅผ ์ด์šฉํ•œ ๋ฆฌ์Šค๋„ˆ ๋“ฑ๋ก</h3>
<hr>
<p id="p">๋งˆ์šฐ์Šค ์˜ฌ๋ฆฌ๋ฉด orchid ์ƒ‰์œผ๋กœ ๋ณ€๊ฒฝ</p>
</body>
</html>

 

ex9-04.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>์ต๋ช… ํ•จ์ˆ˜๋กœ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ์ž‘์„ฑ</title>
<script>
let p;
function init() { // ๋ฌธ์„œ๊ฐ€ ์™„์ „ํžˆ ๋กœ๋“œ๋˜์—ˆ์„ ๋•Œ ํ˜ธ์ถœ
	p = document.getElementById("p");
	p.onmouseover = function () { // ์ต๋ช… ํ•จ์ˆ˜
		this.style.backgroundColor = "orchid";
	};
	p.addEventListener("mouseout",
		function () { this.style.backgroundColor="white"; } // ์ต๋ช… ํ•จ์ˆ˜
	);
}
</script>
</head>
<body onload="init()">
<h3>์ต๋ช… ํ•จ์ˆ˜๋กœ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ์ž‘์„ฑ</h3>
<hr>
<p id="p">๋งˆ์šฐ์Šค ์˜ฌ๋ฆฌ๋ฉด orchid ์ƒ‰์œผ๋กœ ๋ณ€๊ฒฝ</p>
</body>
</html>

 

ex9-05.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>์ด๋ฒคํŠธ ๊ฐ์ฒด ์ „๋‹ฌ๋ฐ›๊ธฐ</title>
</head>
<body>
<p id="p">๋งˆ์šฐ์Šค๋ฅผ ์˜ฌ๋ ค ๋ณด์„ธ์š”</p>
<button onclick="f(event)">ํด๋ฆญํ•˜์„ธ์š”</button>
<script>
function f(e) { // e๋Š” ํ˜„์žฌ ๋ฐœ์ƒํ•œ ์ด๋ฒคํŠธ ๊ฐ์ฒด
	alert(e.type); // ์ด๋ฒคํŠธ ์ข…๋ฅ˜ ์ถœ๋ ฅ
}

document.getElementById("p").onmouseover = f; // ์ž๋™์œผ๋กœ ์ด๋ฒคํŠธ ๊ฐ์ฒด ๋„˜๊ฒจ์คŒ
</script>
</body>
</html>

 

ex9-06.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>์ด๋ฒคํŠธ ๊ฐ์ฒด ํ”„๋กœํผํ‹ฐ</title>
</head>
<body>
<h3>์ด๋ฒคํŠธ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ ์ถœ๋ ฅ</h3>
<hr>
<p id="p">๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์ด๋ฒคํŠธ ๊ฐ์ฒด๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.</p>
<button onclick="f(event)">ํด๋ฆญํ•˜์„ธ์š”</button>
<script>
function f(e) { // e๋Š” ํ˜„์žฌ ๋ฐœ์ƒํ•œ ์ด๋ฒคํŠธ ๊ฐ์ฒด
	let text = "type: " + e.type + "<br>"
			+ "target: " + e.target + "<br>"
			+ "currentTarget: " + e.currentTarget + "<br>"
			+ "defaultPrevented: " + e.defaultPrevented;

	let p = document.getElementById("p");
	p.innerHTML = text; // ์ด๋ฒคํŠธ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ ์ถœ๋ ฅ
}
</script>
</body>
</html>

 

 

ex9-07.html

์ด๋ฒคํŠธ์˜ ๋””ํดํŠธ ํ–‰๋™ ์ทจ์†Œ

์ด๋ฒคํŠธ์˜ ๋””ํดํŠธ ํ–‰๋™ ์ทจ์†Œ


๋„ค์ด๋ฒ„๋กœ ์ด๋™ํ•  ์ง€ ๋ฌผ์–ด๋ณด๋Š” ๋งํฌ
๋นต(์ฒดํฌ ๋จ)
์ˆ (์ฒดํฌ ์•ˆ๋จ)

 

ex9-08.html

<!DOCTYPE html>
<html><head>
<meta charset="UTF-8">
<title>์ด๋ฒคํŠธ ํ๋ฆ„</title></head>
<body>
<p style="color:blue">์ด๊ฒƒ์€
	<span style="color:red" id="span">๋ฌธ์žฅ์ž…๋‹ˆ๋‹ค.
	</span>
</p>
<form>
	<input type="text" name="s">
	<input type="button" value="ํ…Œ์ŠคํŠธ" id="button">
	<hr>
</form>
<div id="div" style="color:green"></div>
<script>
let div = document.getElementById("div"); // ์ด๋ฒคํŠธ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ ๊ณต๊ฐ„
let button = document.getElementById("button");

// body ๊ฐ์ฒด์— ์บก์ณ ๋ฆฌ์Šค๋„ˆ ๋“ฑ๋ก
document.body.addEventListener("click", capture, true); // ์ผญ์ณ ๋‹จ๊ณ„(1)

// ํƒ€๊ฒŸ ๊ฐ์ฒด์— ๋ฒ„๋ธ” ๋ฆฌ์Šค๋„ˆ ๋“ฑ๋ก
button.addEventListener("click", bubble, false); // ๋ฒ„๋ธ” ๋‹จ๊ณ„(2)

// body ๊ฐ์ฒด์— ๋ฒ„๋ธ” ๋ฆฌ์Šค๋„ˆ ๋“ฑ๋ก
document.body.addEventListener("click", bubble, false); // ๋ฒ„๋ธ” ๋‹จ๊ณ„(3)

function capture(e) { // e๋Š” ์ด๋ฒคํŠธ ๊ฐ์ฒด
	let obj = e.currentTarget; // ํ˜„์žฌ ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›์€  DOM ๊ฐ์ฒด
	let tagName = obj.tagName; // ํƒœ๊ทธ ์ด๋ฆ„
	div.innerHTML += "<br>capture ๋‹จ๊ณ„ : " + tagName + " ํƒœ๊ทธ " + e.type + "์ด๋ฒคํŠธ";
}

function bubble(e) { // e๋Š” ์ด๋ฒคํŠธ ๊ฐ์ฒด
	let obj = e.currentTarget; // ํ˜„์žฌ ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›์€  DOM ๊ฐ์ฒด
	let tagName = obj.tagName; // ํƒœ๊ทธ ์ด๋ฆ„
	div.innerHTML += "<br>bubble ๋‹จ๊ณ„ : " + tagName + " ํƒœ๊ทธ " + e.type + "์ด๋ฒคํŠธ";
}
</script>
</body>
</html>

 

ex9-09.html

<!DOCTYPE html>
<html><head>
<meta charset="UTF-8">
<title>onclick</title>
<script>
function calculate() {
	let exp = document.querySelector("#exp");
	let result = document.querySelector("#result");
	result.value = eval(exp.value);
}
</script>
</head>
<body >
<h3> onclick, ๊ณ„์‚ฐ๊ธฐ ๋งŒ๋“ค๊ธฐ</h3>
<hr>
๊ณ„์‚ฐํ•˜๊ณ ์ž ํ•˜๋Š” ์ˆ˜์‹์„
์ž…๋ ฅํ•˜๊ณ  ๊ณ„์‚ฐ ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ๋ด์š”.
<br>
<br>
<form>
์‹ <input type="text" id="exp" value=""><br>
๊ฐ’ <input type="text" id ="result">
<input type="button" value=" ๊ณ„์‚ฐ  "
	onclick="calculate()">
</form>
</body>
</html>

 

ex9-10.html

<!DOCTYPE html>
<html><head>
<meta charset="UTF-8">
<title>๋งˆ์šฐ์Šค ๊ด€๋ จ ๋ฆฌ์Šค๋„ˆ</title>
<script>
let width=1; // ํ…Œ๋‘๋ฆฌ ๋‘๊ป˜
function down(obj) {
	obj.style.fontStyle = "italic";
}
function up(obj) {
	obj.style.fontStyle = "normal";
}
function over(obj) {
	obj.style.borderColor = "violet";
	// ํ…Œ๋‘๋ฆฌ ํญ์ด 0์ผ ๋•Œ ์ƒ‰์€ ๋ณด์ด์ง€ ์•Š๋Š”๋‹ค.
}
function out(obj) {
	obj.style.borderColor = "lightgray";
}
function wheel(e, obj) { // e๋Š” ์ด๋ฒคํŠธ ๊ฐ์ฒด
	if(e.wheelDelta < 0) { // ํœ ์„ ์•„๋ž˜๋กœ ๊ตด๋ฆด ๋•Œ
		width--; // ํญ 1 ๊ฐ์†Œ
		if(width < 0) width = 0; // ํญ์ด 0๋ณด๋‹ค ์ž‘์•„์ง€์ง€ ์•Š๊ฒŒ
	}
	else // ํœ ์„ ์œ„๋กœ ๊ตด๋ฆด ๋•Œ
		width++; // ํญ 1 ์ฆ๊ฐ€
	obj.style.borderStyle = "ridge";
	obj.style.borderWidth = width+"px";
}
</script>
</head>
<body >
<h3>๋งˆ์šฐ์Šค ๊ด€๋ จ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ</h3>
<hr>
<div>๋งˆ์šฐ์Šค ๊ด€๋ จ
	<span onmousedown="down(this)"
		  onmouseup="up(this)"
		  onmouseover="over(this)"
		  onmouseout="out(this)"
		  onwheel="wheel(event, this)"
		  style="display:inline-block">์ด๋ฒคํŠธ
	 </span>๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
</div>
</body>
</html>

 

ex9-11.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ</title>
<style>
div {
	background : skyblue;
	width : 250px;
}
</style>
</head>
<body>
<h3>๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ์™€ onmousemove</h3>
<hr>
์ด๋ฏธ์ง€ ์œ„์— ๋งˆ์šฐ์Šค๋ฅผ ์›€์ง์ผ ๋•Œ onmousemove ๋ฆฌ์Šค๋„ˆ๊ฐ€ ์‹คํ–‰๋˜๊ณ ,
๋งˆ์šฐ์Šค์˜ ์œ„์น˜๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.<br><br>
<img src="media/beach.png" onmousemove="where(event)"><br><br>
<div id="div"></div>
<script>
let div = document.getElementById("div");
function where(e) {
	let text = "๋ฒ„ํŠผ=" + e.button + "<br>";
	text += "(screenX, screenY)=" + e.screenX + "," + e.screenY + "<br>";
	text += "(clientX, clientY)=" + e.clientX + "," + e.clientY + "<br>";
	text += "(offsetX, offsetY)=" + e.offsetX + "," + e.offsetY + "<br>";
	text += "(x, y)=" + e.x + "," + e.y + "\n";
	div.innerHTML = text;
}
</script>
</body>
</html>

 

ex9-12.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>oncontextmenu</title>
<script>
function hideMenu() {
	alert("์˜ค๋ฅธ์ชฝ ํด๋ฆญ<์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด>๊ธˆ์ง€");
	return false;
}
document.oncontextmenu=hideMenu;
</script>
</head>
<body>
<h3>oncontextmenu์—์„œ ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด ๊ธˆ์ง€</h3>
<hr>
๋งˆ์šฐ์Šค ์˜ค๋ฅธ์ชฝ ํด๋ฆญ์€ ๊ธˆ์ง€๋ฉ๋‹ˆ๋‹ค. ์•„๋ฌด๊ณณ์ด๋‚˜
ํด๋ฆญํ•ด๋„ ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด๋ฅผ ๋ณผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.<br>
<img src="media/beach2.png" alt="miami">
</body>
</html>

 

ex9-13.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML ๋ฌธ์„œ์˜ onload</title>
</head>
<body onload="alert('์ด ์‚ฌ์ดํŠธ๋Š” 2022๋…„ 9์›”1์ผ๋ถ€ํ„ฐ \
www.js.co.kr๋กœ ์˜ฎ๊ฒจ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.')">
<h3>HTML ๋ฌธ์„œ์˜ ๋กœ๋”ฉ ์™„๋ฃŒ, onload</h3>
<hr>
์ด ํŽ˜์ด์ง€๋Š” onload ๋ฆฌ์Šค๋„ˆ์˜ ์‚ฌ์šฉ ์˜ˆ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค
์ด ํŽ˜์ด์ง€๊ฐ€ ์ถœ๋ ฅ๋˜๊ณ  ๋‚œ ๋ฐ”๋กœ ์งํ›„ onload ๋ฆฌ์Šค๋„ˆ๋ฅผ ํ†ตํ•ด
๊ฒฝ๊ณ ์ฐฝ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
</body>
</html>

 

ex9-14.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>onload๋กœ ์ด๋ฏธ์ง€ ํฌ๊ธฐ ์ถœ๋ ฅ</title>
<script>
function changeImage() {
	let sel = document.getElementById("sel");
	let img = document.getElementById("myImg");
	img.onload = function () {
		 // ์ด๋ฏธ์ง€ ํฌ๊ธฐ ์ถœ๋ ฅ
	 	let mySpan = document.getElementById("mySpan");
		mySpan.innerHTML = img.width + "x" + img.height;
	}
	let index= sel.selectedIndex; // ์„ ํƒ๋œ ์˜ต์…˜ ์ธ๋ฑ์Šค
	img.src = sel.options[index].value; // <option>์˜ value ์†์„ฑ
	// ์ด๋ฏธ์ง€ ๋กœ๋”ฉ ์‹œ์ž‘, ์™„๋ฃŒ ํ›„ onload ๋ฆฌ์Šค๋„ˆ ํ˜ธ์ถœ
}
</script>
</head>
<body onload="changeImage()">
<h3>onload๋กœ ์ด๋ฏธ์ง€ ํฌ๊ธฐ ์ถœ๋ ฅ</h3>
<hr>
<form>
<select id="sel" onchange="changeImage()">
	<option value="media/apple.png">์‚ฌ๊ณผ
	<option value="media/banana.png">๋ฐ”๋‚˜๋‚˜
	<option value="media/mango.png">๋ง๊ณ 
</select>
<span id="mySpan">์ด๋ฏธ์ง€ ํฌ๊ธฐ</span>
</form>
<p><img id="myImg" src="media/apple.png" alt="."></p>
</body>
</html>

 

ex9-15.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>new Image()๋กœ ์ด๋ฏธ์ง€ ๋กœ๋”ฉ</title>
<script>
// ๋ฏธ๋ฆฌ ๋กœ๋”ฉํ•ด ๋‘˜ 8๊ฐœ์˜ ์ด๋ฏธ์ง€ ํŒŒ์ผ ์ด๋ฆ„ ๋ฐฐ์—ด
let files = ["media/Penguins.png",
             "media/Lighthouse.png",
             "media/Chrysanthemum.png",
             "media/Desert.png",
             "media/Hydrangeas.png",
             "media/Jellyfish.png",
             "media/Koala.png",
             "media/Tulips.png"];
let imgs = new Array(); // ์ด๋ฏธ์ง€ ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด
for(let i=0; i<files.length; i++) {
	imgs[i] = new Image(); // ์ด๋ฏธ์ง€ ๊ฐ์ฒด ์ƒ์„ฑ
	imgs[i].src = files[i]; // ์ด๋ฏธ์ง€ ๋กœ๋”ฉ ์ง€์‹œ
}

let next = 1;
function change(img) { // ๋‹ค์Œ ์ด๋ฏธ์ง€ ์ถœ๋ ฅ
	img.src = imgs[next].src; // ์ด๋ฏธ์ง€ ๋ณ€๊ฒฝ
	next++; // ๋‹ค์Œ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ์ธ๋ฑ์Šค
	next %= imgs.length; // ๊ฐœ์ˆ˜๋ฅผ ๋„˜์œผ๋ฉด ์ฒ˜์Œ์œผ๋กœ
}
</script>
</head>
<body>
<h3>new Image()๋กœ ์ด๋ฏธ์ง€ ๋กœ๋”ฉ</h3>
<hr>
์ด๋ฏธ์ง€๋ฅผ ํด๋ฆญํ•˜๋ฉด ๋‹ค์Œ ์ด๋ฏธ์ง€๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.<p>
<img style="border:20px ridge wheat"
	src="media/Penguins.png" alt="."
	width="200" height="200"
	onclick="change(this)">
</body>
</html>

 

ex9-16.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>onfocus์™€ onblur</title>
<script>
function checkFilled(obj) {
	if(obj.value == "") { // obj์— ์ž…๋ ฅ๋œ ๊ฒƒ์ด ์—†๋‹ค๋ฉด
		obj.focus(); // obj์— ๋‹ค์‹œ ํฌ์ปค์Šค
	}
}
</script>
</head>
<body onload=
	"document.getElementById('name').focus();">
<h3>onfocus์™€ onblur</h3>
<hr>
<p>์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์ง€ ์•Š๊ณ  ๋‹ค๋ฅธ ์ฐฝ์œผ๋กœ
์ด๋™ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.</p>
<form>
์ด๋ฆ„ <input type="text" id="name"
			onblur="checkFilled(this)"><p>
ํ•™๋ฒˆ <input type="text">
</form>
</body>
</html>

ex9-17.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>์„ ํƒ๋œ ๋ผ๋””์˜ค๋ฒ„ํŠผ ์•Œ์•„๋‚ด๊ธฐ</title>
<script>
function findChecked() {
	let found = null;
	let kcity = document.getElementsByName("city");
	for(let i=0; i<kcity.length; i++) {
		if(kcity[i].checked == true)
			found = kcity[i];
	}
	if(found != null)
		alert(found.value + "์ด ์„ ํƒ๋˜์—ˆ์Œ");
	else
		alert("์„ ํƒ๋œ ๊ฒƒ์ด ์—†์Œ");
}
</script>
</head>
<body>
<h3>๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์„ ํƒ๋œ ๋ผ๋””์˜ค ๋ฒ„ํŠผ์˜ value๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.</h3>
<hr>
<form>
	<input type="radio" name="city" value="seoul" checked>์„œ์šธ
	<input type="radio" name="city" value="busan">๋ถ€์‚ฐ
	<input type="radio" name="city" value="chunchen">์ถ˜์ฒœ
	<input type="button" value="find checked" onclick="findChecked()">
</form>
</body>
</html>

 

ex9-18.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>์„ ํƒ๋œ ๋ฌผํ’ˆ ๊ณ„์‚ฐํ•˜๊ธฐ</title>
<script>
let sum=0;
function calc(cBox) {
	if(cBox.checked)
		sum += parseInt(cBox.value);
	else
		sum -= parseInt(cBox.value);
	document.getElementById("sumtext").value = sum;
}
</script>
</head>
<body>
<h3>๋ฌผํ’ˆ์„ ์„ ํƒํ•˜๋ฉด ๊ธˆ์•ก์ด ์ž๋™ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค</h3>
<hr>
<form>
	<input type="checkbox" name="hap" value="10000"
		onclick="calc(this)">๋ชจ์ž 1๋งŒ์›
	<input type="checkbox" name="shose" value="30000"
		onclick="calc(this)">๊ตฌ๋‘ 3๋งŒ์›
	<input type="checkbox" name="bag" value="80000"
		onclick="calc(this)">๋ช…ํ’ˆ๊ฐ€๋ฐฉ 8๋งŒ์›<br>
	์ง€๋ถˆํ•˜์‹ค ๊ธˆ์•ก <input type="text" id="sumtext" value="0" >
</form>
</body>
</html>

 

ex9-19.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>select ๊ฐ์ฒด์—์„œ ์„ ํƒํ•œ ๊ณผ์ผ์ถœ๋ ฅ</title>
<script>
function drawImage() {
	let sel = document.getElementById("fruits");
	let img = document.getElementById("fruitimage");
	img.src = sel.options[sel.selectedIndex].value;
}
</script>
</head>
<body onload="drawImage()">
<h3>select ๊ฐ์ฒด์—์„œ ์„ ํƒํ•œ ๊ณผ์ผ ์ถœ๋ ฅ</h3>
<hr>
๊ณผ์ผ์„ ์„ ํƒํ•˜๋ฉด ์ด๋ฏธ์ง€๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.<p>
<form>
<select id="fruits" onchange="drawImage()">
	<option value="media/strawberry.png">๋”ธ๊ธฐ
	<option value="media/banana.png" selected>๋ฐ”๋‚˜๋‚˜
	<option value="media/apple.png">์‚ฌ๊ณผ
</select>
<img id="fruitimage" src="media/banana.gif" alt="">
</form>
</body>
</html>

ex9-20.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ํ‚ค ์ด๋ฒคํŠธ</title>
<script>
function whatKeyDown(e) {
	let str = "";
	let div = document.getElementById("div");
	div.innerHTML = ""; // div ๊ฐ์ฒด ๋‚ด์šฉ ์ดˆ๊ธฐํ™”
	str += "e.key = " + e.key + "<br>";
	str += "e.code = " + e.code + "<br>";
	div.innerHTML = str; // div ๊ฐ์ฒด์— html ๋ฌธ์ž์—ด ์ถœ๋ ฅ
}
</script>
</head>
<body>
<h3>ํ‚ค ๋ฆฌ์Šค๋„ˆ์™€ ํ‚ค ์ด๋ฒคํŠธ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ</h3>
<hr>
ํ…์ŠคํŠธ ์ฐฝ์— ํ‚ค๋ฅผ ๋ˆŒ๋Ÿฌ ๋ณด์„ธ์š”. Alt, Shift, Ctrl ํ‚ค๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.<br>
<input type="text" id="text" onkeydown="whatKeyDown(event)">
<div id="div" style="background-color:skyblue; width:250px; height:50px">
</div>
</body>
</html>

 

ex9-21.html

<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>ํ‚ค ์ด๋ฒคํŠธ ์‘์šฉ</title>
<style>
	td { width:50px; height:50px; border:1px solid orchid;  }
</style>
<script>
let tds;
let prevIndex=0, index=0;
window.onload = function () { // ์›น ํŽ˜์ด์ง€์˜ ๋กœ๋”ฉ ์™„๋ฃŒ์‹œ ์‹คํ–‰
	tds = document.getElementsByTagName("td");
	tds[index].style.backgroundColor = "orchid";
}
window.onkeydown = function (e) {
	switch(e.key) {
		case "ArrowDown" :
			if(index/3 >= 2) return; // ๋งจ ์œ„ ์…€์˜ ๊ฒฝ์šฐ
			index += 3;
			break;
		case "ArrowUp" :
			if(index/3 < 1) return; // ๋งจ ์•„๋ž˜ ์…€์˜ ๊ฒฝ์šฐ
			index -= 3;
			break;
		case "ArrowLeft" :
			if(index%3 == 0) return; // ๋งจ ์™ผ์ชฝ ์…€์˜ ๊ฒฝ์šฐ
			index--;
			break;
		case "ArrowRight" :
			if(index%3 == 2) return; // ๋งจ ์˜ค๋ฅธ์ชฝ ์…€์˜ ๊ฒฝ์šฐ
			index++;
			break;
	}
	tds[index].style.backgroundColor = "orchid";
	tds[prevIndex].style.backgroundColor = "white";
	prevIndex = index;
}
</script></head>
<body>
<h3>์ƒํ•˜์ขŒ์šฐ ํ‚ค๋กœ ์…€ ์ด๋™ํ•˜๊ธฐ</h3><hr>
<table>
	<tr><td></td><td></td><td></td></tr>
	<tr><td></td><td></td><td></td></tr>
	<tr><td></td><td></td><td></td></tr>
</table>
</body></html>

๋Œ“๊ธ€