MapleStory Finger Point

๐ŸŸก JavaScript

JavaScript ๊ธฐ์ดˆ 1ํƒ„

HYEJU01 2024. 4. 27. 02:33

 

getElementById()

  • HTML ์š”์†Œ(id="demo" ํฌํ•จํ•œ) ์ฐพ์•„์„œ, ์š”์†Œ ์ฝ˜ํ…์ธ (innerHTML) ๋ฅผ ๋ณ€๊ฒฝํ•จ
  • ํฐ ๋”ฐ์˜ดํ‘œ, ์ž‘์€ ๋”ฐ์˜ดํ‘œ ๋ชจ๋‘ ํ—ˆ์šฉ

 

<p id="demo">๋ฐ˜๊ฐ‘์Šต๋‹ˆ๋‹ค!</p>

<button type="button" onclick='document.getElementById("demo").innerHTML = "์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ!"'>
Click Me!
</button>

JavaScript Can Change HTML Attribute Values

<button onclick="document.getElementById('myImage').src='pic_bulbon.gif'">Turn on the light</button>

<img id="myImage" src="pic_bulboff.gif" style="width:100px">

<button onclick="document.getElementById('myImage').src='pic_bulboff.gif'">Turn off the light</button>

 

html - src ์†์„ฑ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

JavaScript Can Change HTML Styles (CSS)

<button type="button" onclick="document.getElementById('demo').style.fontSize='35px'">
Click Me!</button>

 

css - style ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.


JavaScript Can Show &  Hide HTML Elements

<button type="button" onclick="document.getElementById('demo').style.display='none'">
Click Me!</button>

<button type="button" onclick="document.getElementById('demo').style.display='block'">
Click Me!</button>

html ์š”์†Œ๋ฅผ ์ˆจ๊ธฐ๊ฑฐ๋‚˜ ๋ณด์ด๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

The <script> Tag

<script> </script> ํƒœ๊ทธ ์‚ฌ์ด์— ์ฝ”๋“œ๊ฐ€ ์‚ฝ์ž…๋œ๋‹ค.


JavaScript Functions and Events

JavaScript function ๋Š” ํ˜ธ์ถœ๋  ๋•Œ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ธ”๋ก์ด๋‹ค.

์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.


JavaScript in <head>

ํ—ค๋” ์•ˆ์— ๋ฐฐ์น˜๋จ



JavaScript in <body>

๋ฐ”๋”” ํƒœ๊ทธ ๋๋ถ€๋ถ„ ์œ„์— ๋ฐฐ์น˜๋จ

 

(+) ์ผ๋ฐ˜์ ์œผ๋กœ <body> ์š”์†Œ์˜ ๋งจ ์•„๋ž˜์— ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋ฐฐ์น˜ํ•˜๋Š” ๊ฒƒ์ด ํŽ˜์ด์ง€์˜ ์ดˆ๊ธฐ ๋ Œ๋”๋ง ์†๋„๋ฅผ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— ํŽ˜์ด์ง€์˜ ๋‚˜๋จธ์ง€ ์ฝ˜ํ…์ธ ๋ฅผ ๋จผ์ € ๋ Œ๋”๋งํ•˜๋ฏ€๋กœ ์‚ฌ์šฉ์ž๋Š” ํŽ˜์ด์ง€๋ฅผ ๋” ๋นจ๋ฆฌ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ํŽ˜์ด์ง€ ๋กœ๋“œ๋ฅผ ์ฐจ๋‹จํ•˜์ง€ ์•Š๊ณ  ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋กœ๋“œ๋˜๋Š” ๊ฒฝ์šฐ์—๋„ ์ด์ ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ชจ๋“  ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ๋งจ ์•„๋ž˜์— ์œ„์น˜ํ•  ํ•„์š”๋Š” ์—†์œผ๋ฉฐ, ํŽ˜์ด์ง€์˜ ํŠน์ • ์š”๊ตฌ์‚ฌํ•ญ์— ๋”ฐ๋ผ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ ์ ˆํ•œ ์œ„์น˜์— ๋ฐฐ์น˜ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.


External JavaScript

์™ธ๋ถ€ ํŒŒ์ผ์— ์œ„์น˜ํ•จ

๋ฐ”๋”” ํƒœ๊ทธ ๋๋ถ€๋ถ„์— ๋ฐฐ์น˜๋จ

 


External JavaScript Advantages

  • HTML๊ณผ ์ฝ”๋“œ๋ฅผ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  • HTML๊ณผ JavaScript๋ฅผ ์ฝ๊ณ  ์œ ์ง€ํ•˜๊ธฐ ์‰ฝ๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
  • ์บ์‹œ๋œ JavaScript ํŒŒ์ผ์€ ํŽ˜์ด์ง€ ๋กœ๋“œ๋ฅผ ๊ฐ€์†ํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํ•œ ํŽ˜์ด์ง€์— ์—ฌ๋Ÿฌ ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์Šคํฌ๋ฆฝํŠธ ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค.


External References

  1. ๋งํฌ
    <script src="https://www.w3schools.com/js/myScript.js"></script>โ€‹
  2. ํŒŒ์ผ๊ฒฝ๋กœ
    <script src="/js/myScript.js"></script>โ€‹
  3. ๊ฒฝ๋กœ ์—†์ด
    <script src="myScript.js"></script>โ€‹