Programing

마우스 오버 이벤트와 마우스 입력 이벤트의 차이점은 무엇입니까?

lottogame 2020. 6. 15. 08:17
반응형

마우스 오버 이벤트와 마우스 입력 이벤트의 차이점은 무엇입니까?


나는 항상 mouseover이벤트를 사용 했지만 jQuery 설명서를 읽는 동안 발견했습니다 mouseenter. 그들은 정확히 같은 기능을하는 것 같습니다.

둘 사이에 차이가 있습니까? 그렇다면 언제 사용해야합니까?
( mouseout에도 적용 mouseleave).


jQuery 문서 페이지 에서 다음 예제를 시도해 볼 수 있습니다 . 아주 명확하고 대화식 데모이며 실제로 볼 수 있습니다.

var i = 0;
$("div.overout")
  .mouseover(function() {
    i += 1;
    $(this).find("span").text("mouse over x " + i);
  })
  .mouseout(function() {
    $(this).find("span").text("mouse out ");
  });

var n = 0;
$("div.enterleave")
  .mouseenter(function() {
    n += 1;
    $(this).find("span").text("mouse enter x " + n);
  })
  .mouseleave(function() {
    $(this).find("span").text("mouse leave");
  });
div.out {
  width: 40%;
  height: 120px;
  margin: 0 15px;
  background-color: #d6edfc;
  float: left;
}

div.in {
  width: 60%;
  height: 60%;
  background-color: #fc0;
  margin: 10px auto;
}

p {
  line-height: 1em;
  margin: 0;
  padding: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="out overout">
  <span>move your mouse</span>
  <div class="in">
  </div>
</div>

<div class="out enterleave">
  <span>move your mouse</span>
  <div class="in">
  </div>
</div>

즉, 하위 요소 또는 상위 요소에서 오는 요소 위에 마우스 오버 이벤트가 발생하지만 마우스 입력 이벤트는 마우스가이 요소 외부에서이 요소로 이동할 때만 발생합니다.

Or as the mouseover() docs put it:

[.mouseover()] can cause many headaches due to event bubbling. For instance, when the mouse pointer moves over the Inner element in this example, a mouseover event will be sent to that, then trickle up to Outer. This can trigger our bound mouseover handler at inopportune times. See the discussion for .mouseenter() for a useful alternative.


Mouseenter and mouseleave do not react to event bubbling, while mouseover and mouseout do.

Here's an article that describes the behavior.


As is often true with questions like these, Quirksmode has the best answer.

I would imagine that, because one of jQuery's goals is to make things browser agnostic, that using either event name will trigger the same behavior. Edit: thanks to other posts, I now see this is not the case


$(document).ready(function() {
$("#outer_mouseover").bind
("Mouse Over Mouse Out",function(event){
console.log(event.type," :: ",this.id);})
$("#outer_mouseenter").bind
("Mouse enter Mouse leave",function(event){
console.log(event.type," :: ",this.id);})
 });

참고URL : https://stackoverflow.com/questions/1104344/what-is-the-difference-between-the-mouseover-and-mouseenter-events

반응형