<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Events</title>
</head>
<body>
<h1>JavaScript Events</h1>
<a href="http://google.com">Link to Google</a>
<script>
var link = document.querySelector('a');
link.addEventListener('click', function(event){
event.preventDefault();
alert("Sorry, no google for you!");
});
</script>
</body>
</html>
event.preventDefault();를 넣어주면
Link to Google를 클릭해도 google.com으로 가는 코드가 실행되지 않음
Event.preventDefault() - Web APIs | MDN
The preventDefault() method of the Event interface tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be.
developer.mozilla.org
'Programming > JavaScript' 카테고리의 다른 글
Capturing Scroll Events (0) | 2022.03.11 |
---|---|
Mouseover, Mouseover Events: HTMLPreviousNext (0) | 2022.03.11 |
Sequence selection loop (0) | 2022.03.09 |
While Loops (0) | 2022.03.09 |
Difference Between == and === in JavaScript (0) | 2022.03.08 |