[WEB] Javascript ์‹ค์Šต

HyeJi9908ยท2022๋…„ 9์›” 13์ผ
0

[WEB] ๊ธฐ์ดˆ

๋ชฉ๋ก ๋ณด๊ธฐ
4/8

๐ŸŽˆ ์‹ค์Šต1 - ๋ฒ„ํŠผ ์ƒ์„ฑ

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script>
      alert("hello world");		-> ๊ฒฝ๊ณ ์ฐฝ ์ƒ์„ฑ
    </script>
  </head>
  <body>
    <input type="button" value="white" onclick="alert('Hello world!')"/>		-> ๋ฒ„ํŠผ ์ƒ์„ฑ ๋ฐ ํด๋ฆญ ์ด๋ฒคํŠธ๋กœ ๊ฒฝ๊ณ ์ฐฝ ์ƒ์„ฑ
    <input type="text" onfocus="alert('focus')" onblur="alert('blur')">			-> ํ…์ŠคํŠธ ํ•„๋“œ ๋ฐ ํ•„๋“œ ํด๋ฆญ์‹œ ๊ฒฝ๊ณ ์ฐฝ ์ƒ์„ฑ
  </body>
</html>

๐ŸŽˆ ์‹ค์Šต2 - ์‚ฌ์šฉ์ž๊ฐ€ ํ…์ŠคํŠธ ํ•„๋“œ์— ์ž…๋ ฅํ•œ ๊ฐ’์„ ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ํ†ตํ•ด ๊ฒฝ๊ณ ์ฐฝ์— ๋„์šฐ๊ธฐ

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script>
      alert("hello world");
    </script>
  </head>
  <body>
    <input type="text" id = "user_input"/>		-> ์‚ฌ์šฉ์ž๊ฐ€ ํ…์ŠคํŠธ ํ•„๋“œ์— ์ž…๋ ฅํ•œ ๊ฐ’์„ 'user_input' ์ด๋ผ๋Š” id๋กœ ์ €์žฅ
    <input type="button" value="white" onclick="alert(document.getElementById('user_input').value)">		-> ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ๋กœ, ๊ฒฝ๊ณ ์ฐฝ ๋„์›€ + ๊ฒฝ๊ณ ์ฐฝ์— ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ํ…์ŠคํŠธ๋ฅผ id๋กœ ๊ฐ€์ ธ์™€ ํ‘œ์‹œํ•จ
  </body>
</html>

๐ŸŽˆ ์‹ค์Šต3 - ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ํŠน์ • ํƒœ๊ทธ์˜ id๊ฐ’์„ ๊ฐ€์ ธ์™€ ํด๋ž˜์Šค ์ง€์ •ํ•˜๊ธฐ(feat. css)

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <style>						-> css
      li{
        color:blue;
      }
      ul li{
        font-size:40px;
      }
      .em{						-> em์ด๋ผ๋Š” ํด๋ž˜์Šค์˜ css
        text-decoration:underline;
      }
    </style>
  </head>
  <body>
    <ol id ="target">			-> olํƒœ๊ทธ์— id ๋ถ€์—ฌ
      <li>html</li>
      <li>css</li>
      <li>JavaScript</li>
    </ol>
    <ul>
      <li>์ตœ์ง„ํ˜</li>
      <li>์ตœ์œ ๋นˆ</li>
      <li>ํ•œ์•„๋žŒ</li>
      <li>ํ•œ์ด์€</li>
    </ul>
    <input type="button" value="๊ฐ•์กฐ"		
       onclick="document.getElementById('target').className='em'"/>			-> ๋ฒ„ํŠผ ํด๋ฆญ์‹œ target์ด๋ผ๋Š” id๋ฅผ ๊ฐ€์ ธ์™€ ํ•ด๋‹น ํƒœ๊ทธ์— emํด๋ž˜์Šค๋กœ ์ง€์ •๋˜๊ฒŒ๋” ํ•จ + emํด๋ž˜์Šค์— ์ง€์ •๋จ์œผ๋กœ์จ ํ•ด๋‹น ํƒœ๊ทธ์— emํด๋ž˜์Šค์˜ css์ธ ์–ธ๋”๋ผ์ธ css ์ ์šฉ๋จ
  </body>
</html>

๐ŸŽˆ ์‹ค์Šต4 - ๋ฒ„ํŠผ ์ด๋ฒคํŠธ๋กœ ๋ฐฐ๊ฒฝ์ƒ‰ ๋ณ€๊ฒฝ

(+ navํƒœ๊ทธ์™€ articleํƒœ๊ทธ ์‚ฌ์ด ๋ฒ„ํŠผ์„ ์œ„์น˜์‹œ์ผœ์•ผ ํ•œ๋‹ค)

  • page_vc.html
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<link rel="stylesheet" type="text/css" href="http://localhost/style.css">
</head>
<body id = "target">					-> body ํƒœ๊ทธ์— id ์„ค์ •
	<header>
		<h1><a href="http://localhost/">JavaScript</a></h1>
	</header>
	<nav>
		<ol>
			<li><a href="http://localhost/page_html.html">JavaScript๋ž€?</a></li>
			<li><a href="http://localhost/page_vc.html">๋ณ€์ˆ˜์™€ ์ƒ์ˆ˜</a></li>
			<li><a href="http://localhost/page_op.html">์—ฐ์‚ฐ์ž</a></li>
		</ol>
	</nav>
	
    -> ๋ฒ„ํŠผ์œ„์น˜๋ฅผ ๊ณ ๋ คํ•˜์—ฌ navํƒœ๊ทธ์™€ articleํƒœ๊ทธ ์‚ฌ์ด ์œ„์น˜์‹œํ‚ค๊ธฐ
	<div id = "control">						-> ๋ฒ„ํŠผ์„ ๊ทธ๋ฃนํ•‘ํ•˜๊ธฐ ์œ„ํ•ด div ํƒœ๊ทธ๋กœ ๋ฌถ๊ธฐ + id ์„ค์ •
		<input type="button" value="white" onclick="document.getElementById('target').className='white'"/>
		<input type="button" value="black" onclick="document.getElementById('target').className='black'"/>
	</div>								-> ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด target id๋ฅผ ๊ฐ€์ ธ์™€ cssํŒŒ์ผ์˜ 'className'์ด๋ฆ„์˜ ํด๋ž˜์Šค ์ ์šฉ

	<article>
		<h2>๋ณ€์ˆ˜์™€ ์ƒ์ˆ˜</h2>
		๋ณ€์ˆ˜๋ž€

		<ul>
			<li>	๋ณ€์ˆ˜๋Š” ๋ฐ”๋€” ์ˆ˜ ์žˆ๋Š” ๊ฐ’์ด๊ณ , ์ƒ์ˆ˜๋Š” ๋ฐ”๋€Œ์ง€ ์•Š๋Š” ๊ฐ’์ž…๋‹ˆ๋‹ค.</li>
			<li>  ๋ณ€ํ•˜๋Š” ๊ฐ’</li>
		</ul>

		์ƒ์ˆ˜๋ž€
		<ul>
			<li>	๋ณ€ํ•˜์ง€ ์•Š๋Š” ๊ฐ’</li>
			<li>  a=10 ์ผ ๋•Œ ์˜ค๋ฅธ์ชฝ ๊ฐ’์ธ 10์ด ์ƒ์ˆ˜๊ฐ€ ๋œ๋‹ค.</li>
 		</ul>


	</article>
</body>
</html>
  • style.css
body.white{				-> bodyํƒœ๊ทธ์— ํŠน์ • ํด๋ž˜์Šค ์ด๋ฆ„ white๋กœ ์„ค์ •
  background-color: white;
  color: black;				-> ๊ธ€
  ์‹œ ์ƒ‰ ๊ฒ€์ •
}

body.black{				-> bodyํƒœ๊ทธ์— ํŠน์ • ํด๋ž˜์Šค ์ด๋ฆ„ black ์„ค์ •
background-color: black;
color: white;
}

header{
  border-bottom:1px solid gray;
  padding:20px;
}
nav{
  border-right: 1px solid gray;
  width:200px;
  height: 600px;
  float: left;
}
nav ol{
  list-style: none;
}
article{
  float: left;
  padding:20px;
}
h2{
  font-size: 50px;

}
#control{		-> htmlํŒŒ์ผ ๋‚ด control ์ด๋ฆ„์˜ ์•„์ด๋””๋ฅผ ๊ฐ€์ง„ ํƒœ๊ทธ css
  float: right;			-> ์˜ค๋ฅด์ชฝ์— ์œ„์น˜์‹œํ‚ค๊ธฐ
}

0๊ฐœ์˜ ๋Œ“๊ธ€