JS๋ฅผ ๋ถ๋ฌ์ค๋ ๋ ๊ฐ์ง ๋ฐฉ์ (์ ์ญ๋ฒ์, ES6 ๋ชจ๋ ์์คํ )
์์ฆ์ ์น์ฌ์ดํธ ๊ฐ๋ฐ์ ๋๊ตฌ ๋๋ ํ์ด์ง ์์ค ๋ณด๊ธฐ๋ฅผ ํตํด HTML๋ฅผ ํ๊ตฌ๋ฅผ ๋ง์ด ํ๊ณ ์๋ค.
๊ทผ๋ฐ ๋ฌธ๋ฉ HTML์ ๋ถ๋ฌ์ค๋ JS ํํ์ ๋ํด ๊ถ๊ธํด์ก๋ค.
๋ฌธ์ ๊ฐ ๋ฐ์ํ์ ๋ ๋๋ฒ๊น ์ ํตํด ๋๋ ๋คํธ์ํฌ ํญ์ ํตํด ๊ฐ ์์ฒญ, ์๋ต์ ๋ณด๋ ๊ณผ์ ์์ ๋ด๊ฐ ์ ํํ๊ฒ ์ด๋ป๊ฒ ๋ณด๊ณ ์๋์ง๊ฐ ๊ถ๊ธํด์ก๋ ๊ฒ์ด๋ค.
HTML์๋ JavaScript ํ์ผ์ ๋ถ๋ฌ์ฌ ๋ ๋ ๊ฐ์ง ๋ฐฉ์์ด ์๋ค.
1. ์ ํต์ ์ธ ๋ฐฉ์ (์ ์ญ ๋ฒ์)
HTML์์ JavaScript ํ์ผ์ ๋ถ์ด์ฌ ๋, <script> ํ๊ทธ๋ฅผ ์ฌ์ฉํ์ฌ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ ์ ์๋ค.
์ ํต์ ์ธ ๋ฐฉ์์ ์์
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>์ ํต์ ์ธ JS ๋ก๋</title>
<script src="script1.js"></script> <!-- script1.js ๋ก๋ -->
<script src="script2.js"></script> <!-- script2.js ๋ก๋ -->
</head>
<body>
<h1>์ ํต์ ์ธ JS ๋ก๋ ์์</h1>
<button onclick="anotherFunction()">ํจ์ ํธ์ถ</button> <!-- script2.js์ ํจ์ ํธ์ถ -->
</body>
</html>
์ด ๊ฒฝ์ฐ, script1.js์ ์ ์๋ ํจ์์ ๋ณ์๋ฅผ ์ ์ญ์์ ์ฌ์ฉํ ์ ์๋ค. ์๋ฅผ ๋ค์ด, script1.js์ sharedFunction()์ ์ ์ํ๊ณ , script2.js์์ ์ด๋ฅผ ํธ์ถํ ์ ์๋ค.
script1.js
function sharedFunction() {
console.log("๊ณต์ ๋ ํจ์ ํธ์ถ๋จ!");
}
script2.js
function anotherFunction() {
sharedFunction(); // script1.js์ ํจ์ ํธ์ถ
}
์ด๋ ๊ฒ import ๊ณผ์ ์์ด ์ฌ์ฉํ ์ ์๋ค.
ํ์ง๋ง, ์ด ๋ฐฉ์์ ์ ์ญ์ผ๋ก ์งํ๋๊ธฐ ๋๋ฌธ์ ์ค๋ณต๋๋ ํจ์ ๋ฑ์ผ๋ก ์ธํด ์ถฉ๋์ด ์ผ์ด๋ ์๋ ์๋ค.]
2. ES6 ๋ชจ๋ ์์คํ
ES6 ๋ชจ๋ ์์คํ ์ ์ฌ์ฉํ ๊ฒฝ์ฐ, ๊ฐ ๋ชจ๋์์ export ์ import๋ฅผ ์ฌ์ฉํ์ฌ ๋ช ์์ ์ผ๋ก ํจ์๋ฅผ ๋ด๋ณด๋ด๊ณ ๊ฐ์ ธ์์ผ ํ๋ค.
ES6 ๋ชจ๋ ์ฌ์ฉ ์์
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ES6 ๋ชจ๋ ๋ก๋</title>
<script type="module" src="script2.js"></script> <!-- script2.js๋ฅผ ๋ชจ๋๋ก ๋ก๋ -->
</head>
<body>
<h1>ES6 ๋ชจ๋ ๋ก๋ ์์</h1>
<button onclick="anotherFunction()">ํจ์ ํธ์ถ</button>
</body>
</html>
script1.js
// script1.js
export function sharedFunction() {
console.log("๊ณต์ ๋ ํจ์ ํธ์ถ๋จ!");
}
script1.js
// script2.js
import { sharedFunction } from './script1.js'; // script1.js์์ ํจ์ ๊ฐ์ ธ์ค๊ธฐ
export function anotherFunction() {
sharedFunction(); // ๊ฐ์ ธ์จ ํจ์ ํธ์ถ
}
์์ฝํ์๋ฉด,
- ์ ํต์ ์ธ ๋ฐฉ์ : <script> ํ๊ทธ๋ก JSํ์ผ์ ๋ถ๋ฌ์ค๋ฉด ํด๋น ํ์ผ์์ ์ ์ํ ๋ชจ๋ ํจ์์ ๋ณ์๋ฅผ ์ ์ญ์์ ์ฌ์ฉํ ์ ์๋ค. ์ด ๊ฒฝ์ฐ export์ import ๊ฐ ํ์๊ฐ ์๋ค.
- ES6 ๋ชจ๋ ์์คํ : ์ด ๋ฐฉ์์ ์ฌ์ฉํ๋ฉด ํ์ผ ๊ฐ์ ์์กด์ฑ์ ๋ช ์์ ์ผ๋ก ์ ์ํ ์ ์์ผ๋ฉฐ, export์ import๋ฅผ ์ฌ์ฉํ์ฌ ํจ์์ ๋ณ์๋ฅผ ๊ณต์ ํด์ผ ํ๋ค. ๋ชจ๋์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ์ญ ์ค์ฝํ์ ๋ ๋ฆฝ์ ์ด๋ฏ๋ก, ๋ณ์ ์ถฉ๋์ ๋ฐฉ์งํ๊ณ ์ฝ๋์ ๊ฐ๋ ์ฑ์ ๋์ผ ์ ์๋ค.
์ข ๋ ๊ตฌ์ฒด์ ์ธ ์์
ES6 ๋ชจ๋์ ์ฌ์ฉํ์ฌ HTML์์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฅ๋ฐ๊ณ , ์ด๋ฅผ POST ์์ฒญ์ผ๋ก ์ ์กํ๋ ์์ ๋ฅผ ๋ง๋ค์ด ๋ณด์๋ค.
1. api.js - POST ์์ฒญ์ ์ฒ๋ฆฌํ๋ ๋ชจ๋
์ด ํ์ผ์์๋ POST ์์ฒญ์ ๋ณด๋ด๋ ํจ์๋ฅผ ์ ์ํ๊ณ ๋ด๋ณด๋ธ๋ค.
// api.js
export function sendPostRequest(data) {
fetch('https://example.com/api/endpoint', { // ์ค์ API ์๋ํฌ์ธํธ๋ก ๋ณ๊ฒฝ
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then(response => response.json())
.then(data => {
console.log('์ฑ๊ณต:', data);
})
.catch((error) => {
console.error('์คํจ:', error);
});
}
2. formHandler.js - ํผ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ชจ๋
์ด ํ์ผ์์๋ ์ฌ์ฉ์์ ์ ๋ ฅ์ ๋ฐ๊ณ POST ์์ฒญ์ ๋ณด๋ด๋ ํจ์๋ฅผ ์ ์ํ๋ค.
// formHandler.js
import { sendPostRequest } from './api.js'; // api.js์์ ํจ์ ๊ฐ์ ธ์ค๊ธฐ
export function handleSubmit() {
const name = document.getElementById('name').value; // ์ด๋ฆ ์
๋ ฅ๊ฐ
const age = document.getElementById('age').value; // ๋์ด ์
๋ ฅ๊ฐ
const postData = {
name: name,
age: age
};
sendPostRequest(postData); // POST ์์ฒญ ๋ณด๋ด๊ธฐ
}
3. index.html - HTML ํ์ผ
์ด HTML ํ์ผ์์๋ ์ฌ์ฉ์๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฅ๋ฐ๊ณ , ๋ฒํผ ํด๋ฆญ ์ POST ์์ฒญ์ ๋ณด๋ด๋ ๊ธฐ๋ฅ์ ํฌํจํ๋ค.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>POST ์์ฒญ ์์</title>
<script type="module" src="formHandler.js"></script> <!-- formHandler.js ๋ชจ๋ ๋ก๋ -->
</head>
<body>
<h1>๋ฐ์ดํฐ ์
๋ ฅ</h1>
<label for="name">์ด๋ฆ:</label>
<input type="text" id="name" placeholder="์ด๋ฆ์ ์
๋ ฅํ์ธ์"><br><br>
<label for="age">๋์ด:</label>
<input type="number" id="age" placeholder="๋์ด๋ฅผ ์
๋ ฅํ์ธ์"><br><br>
<button onclick="handleSubmit()">์ ์ก</button> <!-- ๋ฒํผ ํด๋ฆญ ์ handleSubmit ํจ์ ํธ์ถ -->
</body>
</html>
๋ชจ๋ํ์ ์ด์
- ์ฝ๋์ ์ฌ์ฌ์ฉ์ฑ : ํ ๋ฒ ์์ฑํ ํจ์๋ฅผ ๋ค๋ฅธ ๋ชจ๋์์๋ ์ฌ์ฌ์ฉํ ์ ์๋ค.
- ๋ค์์คํ์ด์ค ๊ด๋ฆฌ : ๊ฐ ๋ชจ๋์ ๋ ๋ฆฝ์ ์ผ๋ก ๋์ํ๋ฏ๋ก, ์ ์ญ ๋ณ์์ ์ถฉ๋์ ๋ฐฉ์งํ ์ ์๋ค.
- ์ ์ง ๋ณด์ ์ฉ์ด์ฑ : ๊ฐ ๋ชจ๋์ ๊ธฐ๋ฅ์ด ๋ถ๋ฆฌ๋์ด ์๊ธฐ ๋๋ฌธ์, ์์ ์ด๋ ์ ๋ฐ์ดํธ๊ฐ ์ฉ์ดํฉ๋๋ค.
'๊ฐ๋ฐ ์ง์, ์คํฌ๋ฆฝํธ > HTML + CSS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
<a href="https://exam.com">Example</ a> (3) | 2024.09.21 |
---|---|
display ์์ฑ inline ๊ณผ block (0) | 2024.04.28 |
hidden md:flex md:block (0) | 2024.04.28 |
JSX ์์ฑ ๊ท์น (0) | 2023.12.17 |
SSAS CSS ๋ค์ด์คํ (0) | 2023.12.15 |
๋๊ธ