1-3 자바스크립트 이해
라이프오브파이
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<title>좋아요 버튼 테스트</title>
<style>
body {
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Pretendard",
"Noto Sans KR", sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #f5f5f5;
}
#likeButton {
font-size: 1rem;
padding: 12px 20px;
border-radius: 8px;
border: none;
background-color: #ff4d6a;
color: white;
cursor: pointer;
}
#likeButton:hover {
background-color: #e03f59;
}
</style>
</head>
<body>
<!-- 버튼 영역 -->
<button id="likeButton">좋아요 ❤️</button>
</body>
</html> <!-- 버튼 영역 -->
<button id="likeButton">좋아요 ❤️</button><!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<title>좋아요 버튼 테스트</title>
<style>
body {
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Pretendard",
"Noto Sans KR", sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #f5f5f5;
}
#likeButton {
font-size: 1rem;
padding: 12px 20px;
border-radius: 8px;
border: none;
background-color: #ff4d6a;
color: white;
cursor: pointer;
}
#likeButton:hover {
background-color: #e03f59;
}
</style>
</head>
<body>
<!-- 버튼 영역 -->
<button id="likeButton">좋아요 ❤️</button>
<!-- 자바스크립트 -->
<script>
document.getElementById("likeButton").addEventListener("click", () => {
alert("좋아요를 눌렀습니다!");
});
</script>
</body>
</html>document.getElementById("likeButton").addEventListener("click", () => {
alert("좋아요를 눌렀습니다!");
});let count = 0;count = count + 1;let count = 0;
document.getElementById("likeButton").addEventListener("click", () => {
count = count + 1;
console.log("좋아요 수:", count);
});function sayHello() {
alert("안녕하세요!");
}document.getElementById("greetButton").addEventListener("click", sayHello);document.getElementById("nameInput").addEventListener("input", (e) => {
console.log("입력 중:", e.target.value);
});document.getElementById("message").innerText = "안녕하세요, 자바스크립트!";요소 | 역할 | 비유 |
HTML | 구조를 정의 | 뼈대 |
CSS | 디자인을 담당 | 옷 |
JavaScript | 동작을 제어 | 근육과 신경 |
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<title>3장 자바스크립트 핵심 원리 테스트</title>
<style>
body {
font-family: 'Pretendard', sans-serif;
background: linear-gradient(135deg, #f9fbe7, #e1f5fe);
height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-start;
padding-top: 40px;
}
h1 {
color: #2e7d32;
margin-bottom: 10px;
}
section {
background: #ffffffcc;
border-radius: 12px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
width: 90%;
max-width: 500px;
margin: 20px 0;
padding: 20px;
}
button {
background-color: #81c784;
color: white;
border: none;
border-radius: 8px;
padding: 10px 15px;
font-size: 16px;
cursor: pointer;
transition: background 0.2s;
}
button:hover {
background-color: #66bb6a;
}
input {
padding: 8px 10px;
border: 2px solid #81c784;
border-radius: 6px;
width: 100%;
margin-top: 5px;
font-size: 16px;
}
p {
font-size: 16px;
margin-top: 10px;
color: #333;
}
.counter {
font-size: 18px;
color: #1565c0;
margin-top: 10px;
}
</style>
</head>
<body>
<h1>💡 자바스크립트 핵심 원리 테스트</h1>
<!-- 1️⃣ 변수와 이벤트: 좋아요 카운터 -->
<section>
<h3>1. 변수와 이벤트 (좋아요 버튼)</h3>
<button id="likeButton">좋아요 ❤️</button>
<p class="counter">좋아요 수: <span id="countDisplay">0</span></p>
</section>
<!-- 2️⃣ 함수: 인사하기 -->
<section>
<h3>2. 함수 (인사하기)</h3>
<button id="greetButton">인사하기 👋</button>
</section>
<!-- 3️⃣ 이벤트: 입력 감지 -->
<section>
<h3>3. 이벤트 (입력 중 로그)</h3>
<input type="text" id="nameInput" placeholder="이름을 입력해보세요" />
<p id="inputPreview">입력 결과가 여기에 표시됩니다.</p>
</section>
<!-- 4️⃣ DOM 조작: 텍스트 변경 -->
<section>
<h3>4. DOM 조작 (화면 내용 바꾸기)</h3>
<p id="message">이 문장은 곧 자바스크립트가 바꿀 예정입니다.</p>
<button id="changeTextButton">텍스트 바꾸기 ✨</button>
</section>
<script>
/* ----------------------------------------------------
1️⃣ 변수와 이벤트
---------------------------------------------------- */
let count = 0;
const likeButton = document.getElementById("likeButton");
const countDisplay = document.getElementById("countDisplay");
likeButton.addEventListener("click", () => {
count = count + 1;
console.log("좋아요 수:", count);
countDisplay.textContent = count;
});
/* ----------------------------------------------------
2️⃣ 함수: 인사하기
---------------------------------------------------- */
function sayHello() {
alert("안녕하세요! 😀");
}
document
.getElementById("greetButton")
.addEventListener("click", sayHello);
/* ----------------------------------------------------
3️⃣ 이벤트: 입력 감지
---------------------------------------------------- */
const input = document.getElementById("nameInput");
const preview = document.getElementById("inputPreview");
input.addEventListener("input", (e) => {
const value = e.target.value;
console.log("입력 중:", value);
preview.textContent = value
? `현재 입력 중: ${value}`
: "입력 결과가 여기에 표시됩니다.";
});
/* ----------------------------------------------------
4️⃣ DOM 조작: 텍스트 변경
---------------------------------------------------- */
const message = document.getElementById("message");
const changeTextButton = document.getElementById("changeTextButton");
changeTextButton.addEventListener("click", () => {
message.innerText = "안녕하세요, 자바스크립트가 바꾼 문장입니다! 🌟";
});
</script>
</body>
</html>