[JavaScript] event.preventDefault() 란?

Yuri Lee·2021년 8월 30일
0

preventDefault 란?

  • a 태그나 submit 태그를 누르게 되면 href 를 통해 이동하거나 창이 새로고침하여 실행된다. 이때 preventDefault 를 통해 동작을 막아줄 수 있다.

많이 사용되는 경우

  1. a 태그를 눌렀을 때도 href 링크로 이동하지 않게 할 경우
  2. form 안에 submit 역할을 하는 버튼을 눌렀어도 새로 실행하지 않게 하고 싶을 경우 (submit 은 작동됨)

Example

<html>
<head>
<title>preventDefault 예제</title>

<script type="text/javascript">

function stopDefAction(evt) {
  evt.preventDefault();
}
</script>
</head>

<body>

<p>체크박스 컨트롤을 클릭해 주세요</p>

<form>
<input type="checkbox" onclick="stopDefAction(event);"/>
<label for="checkbox">체크박스</label>
</form>

</body>
</html>

https://developer.mozilla.org/ko/docs/Web/API/Event/preventDefault
https://programming119.tistory.com/100

profile
Step by step goes a long way ✨

0개의 댓글