html 조작과 변경을 담당하는 언어
<script> 태그 안에 작성<script> 태그 안에 작성한 코드는 브라우저 새로고침 시 1번 실행됨document.getElementById('???').style.color = 'red';
- ??? 부분만 바꾸면 html을 변경하고 조작할 수 있음
document.getElementById('???').style.color = 'red'; document.getElementById('???').src = 'profile.jpg'; document.getElementById("hello").style.fontSize = '16px';

- document : 문서
- . : ~의
- getElementById('hello') : id가 hello인 html 요소(element)를 찾기
- innerHTML = 'HI' : 내부 HTML에 '안녕'이라는 문자를 대입
<head> 태그 안에 <link href="main.css" rel="stylesheet">
display:none (visibility : hidden)
: UI를 평소 숨기고 싶을 때
display:block (visibility : visible)
: UI를 보여주고 싶을 때
- 평소에는 알림창 안 보이게 -> 버튼 누르면 알림창 보이게
- x 버튼 누르면 알림창 안 보이게
<div class="alert-box" id="alert"> 알림창 <button onclick="document.getElementById('alert').style.display='none';">❌</button> </div> <button onclick="document.getElementById('alert').style.display='block';">버튼</button>

- function
: 특정 기능을 재사용 하기 위해 모듈화 해놓은 문법
2강 코드 function으로
<body> <div class="alert-box" id="alert"> 알림창 <button onclick="removeAlert()">❌</button> </div> <button onclick="popAlert()">버튼</button> <script> function popAlert (){ document.getElementById('alert').style.display='block'; } function removeAlert() { document.getElementById('alert').style.display='none'; } </script> </body>
<body> <div class="alert-box" id="alert"> 알림창 <button onclick="popAlert('none');">❌</button> </div> <button onclick="popAlert('block');">버튼</button> <script> function popAlert(closeOpen) { document.getElementById('alert').style.display = closeOpen; } </script> </body>
<p class="title1"> 테스트1 </p>
<p class="title1"> 테스트2 </p>
<script>
document.getElementsByClassName('title1')[0].innerHTML = '안녕';
</script>
<body> <div class="alert-box" id="alert"> <p id="title">알림창 </p> <button onclick="popAlert('none');">❌</button> </div> <button onclick="popAlert('block', '아이디를 입력하세요.');">버튼1</button> <button onclick="popAlert('block', '비밀번호를 입력하세요.');">버튼2</button> <script> function popAlert(closeOpen, title) { document.getElementById('alert').style.display = closeOpen; document.getElementById('title').innerHTML = title; } </script> </body>
id가 'close'인 요소를 클릭하면 function 코드를 실행
document.getElementsByName('close').addEventListener('click', function(){ document.getElementById('alter').style.display = 'none'; });
셀렉터로찾은요소.addEventListener('scroll', function(){} );
이 코드에서 두 번째 파라미터 자리에 함수가 들어감
id 명이 잘못되었을 때

함수 명이 잘못되었을 때

코딩애플 JavaScript 입문과 웹 UI개발