DOM

Untitled

DOM 조회하기

<html>
	<body>
		<input id="id-input" type="text" placeholder="아이디 입력" />

		<input id="password-input" type="password" placeholder="비밀번호 입력" />

		<button class="login-submit">제출하기</button>

		<script>
			// 1. getElementById
			const idInput = document.getElementById('id-input');
			const passwordInput = document.getElementById('password-input');

			// 2. getElementsByClassName
			const submitButton = document.getElementsByClassName('login-submit')[0];

			function login() {
				console.log(idInput.value); // 입력란의 현재 값을 가져온다.
				console.log(passwordInput.value); // 입력란의 현재 값을 가져온다.
			}

			// submitButton 요소에 클릭 이벤트 추가
			submitButton.addEventListener('click', login);
		</script>
	</body>
</html>

addEventListener 메소드

<aside> 💡 사용 방법: DOM요소.addEventListener(이벤트 타입, 실행할 함수, 이벤트 옵션)

</aside>

이벤트 타입 발생 시점
click 요소를 클릭 했을때
input (input, select 태그) 입력 및 선택 값이 변경되었을때
load 요소가 웹 문서에 로드 되었을때 (나타났을때)
unload 요소가 웹 문서에서 언로드 되었을때 (사라질때)
scroll 요소를 스크롤 할때
onmouseenter 요소 영역에 마우스 포인터가 들어왔을때 (hover)
onmouseleave 요소 영역에서 마우스 포인터가 나갔을때 (leave)
onmouseover 요소 영역에서 마우스 포인터가 움직일때
onmouseout 요소 영역에서 마우스 포인터가 나갔을때

<aside> 💡 onmouseenter, onmouseleave vs onmouseover, onmouseout 차이점 이벤트를 걸어둔 요소의 자식요소로 마우스를 이동했을때도 onmouseout → onmouseover 호출, 자식요소를 벗어날때도 onmouseout → onmouseover 호출 (이벤트 버블링이란?)

</aside>

DOM 추가하기

DOM 삭제하기

BOM