<script> 태그로 자바스크립트 작성<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>글자색 바꾸기</title>
<style>
body { text-align:center; }
h1 { color:blue; }
</style>
</head>
<body>
<h1 id="heading">자바스크립트</h1>
<p id="text">위 텍스트를 클릭해 보세요</p>
<script>
let heading = document.querySelector('#heading');
heading.onclick = function() {
heading.style.color = "red";
}
</script>
</body>
</html>
<script> 태그를 사용하지 않고 확장자는 *.js로 저장한다.<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>글자색 바꾸기</title>
<style>
body { text-align:center; }
h1 { color:blue; }
</style>
</head>
<body>
<h1 id="heading">자바스크립트</h1>
<p id="text">위 텍스트를 클릭해 보세요</p>
<script src="change-color.js"></script>
</body>
</html>
식과 문
식: 표현식, 연산식, 실제 값, 함수 실행 등
문: 명령, 조건문, 제어문, 끝에 세미콜론(;)을 붙인다.
기본 입출력
1) 알림 창 출력: alert("메세지")
2) 확인 창 출력: confirm("메세지")
3) 프롬프트 창에서 입력받기: prompt("메세지")
4) 웹 브라우저 화면 출력 document.write()문: document.write("메세지")
5) 콘솔 창 출력 console.log()문: console.log("메세지")
변수란? 프로그램에서 자료를 담아두는 공간
let 변수명const 상수명자료형이란? 숫자, 문자 등 컴퓨터가 처리할 수 있도록 자료형을 알려준다.
1) 숫자형: 정수와 실수로 나누어 구분한다. typeof 숫자
2) 문자열: 작은따옴표(')나 큰따옴표(")로 묶은 데이터
3) 논리형: 참이나 거짓의 값을 표현하는 자료형
4) undefined 유형과 null 유형
5) 배열: 하나의 변수에 값을 여러 개 저장
배열명["값1", "값2", ...]
인덱스: 배열의 각 요소에 자신만의 방 번호가 할당되는데, 방 번호가 인덱스이다. 0, 1, 2, 3, ...
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>조건문</title>
</head>
<body>
<script>
let userNumber=parseInt(prompt('숫자를 입력하세요.'));
if (userNumber%3==0)
alert('3의 배수입니다.');
else
alert('3의 배수가 아닙니다.');
</script>
</body>
</html>
<script>
var session = prompt("관심 세션을 선택하세요. 1-마케팅, 2-개발, 3-디자인");
switch (session) {
case "1":
document.write("마케팅 세션을 선택하셨습니다.");
break;
case "2":
document.write("개발 세션을 선택하셨습니다.");
break;
case "3":
document.write("디자인 세션을 선택하셨습니다.");
break;
default:
document.write("잘못 입력했습니다.");
}
</script>