Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is $(this) called correctly for click but not on hover?

why does this work by element?

<script>
$(document).ready(function(){
  $("p").click(function(){
    $(this).hide();
  });
});
</script>
</head>
<body>
<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
<p>Click me too!</p>

If you click on each one, it disappears.

But if I code it with a hover..

<script>
$(document).ready(function(){
  $("p").hover(function(){
    $(this).hide();
  });
});
</script>
</head>
<body>
<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
<p>Click me too!</p>

They all disappear together. What's the difference?

like image 623
Renekton_Fan Avatar asked Dec 11 '22 11:12

Renekton_Fan


1 Answers

This is because when you are hovering above the first <p> element, it disappears, and therefore the second <p> element moves up to take its place - this process repeats until all the <p> elements are hidden away because your mouse will always be hovering above them.

like image 101
Terry Avatar answered Dec 14 '22 00:12

Terry