I have a question about example mouse event handling!

Asked 2 years ago, Updated 2 years ago, 104 views

[4-18]In the exercise example, in the code state before starting the exercise,

There's an issue that disappears when you click on a monkey. Even if I think about it, I don't know why the image disappears. <

Here is a link to this example: https://codepen.io/yamoo9/pen/NYgvgG

fast-frontend events

2022-09-22 18:50

1 Answers

Hello NeoDahl, ^ - ^

I'm answering your questions.

The reason why the picture disappears when you click on the monkey is because the hash (#) is set in the href attribute value of the a> element, and Codepen reloads the page every time you click.

<a href="#" class="link is-monkys">
  <img src="monks-frame-normal.jpg" alt="Monkey with folded arms">
</a>

This is the default behavior of the browser, and if you programmatically block the browser's default behavior, you can prevent the monkey picture from disappearing when you click on the monkey.

To block the browser's default behavior, you can block the default behavior by receiving the Event object as a parameter for the event handler (function), and then using the preventDefault() method for the passed Event object.

var monkys = document.querySelector('.link.is-monkys');

// Passing Event Objects as Parameters in Event Handler (Function)  
monkys.onclick = function(event) {

  // Block browser default behavior
  event.preventDefault();

  console.log ("Monkey Click");

};


2022-09-22 18:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.