最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - mouseenter and mouseleave in javascript not working - Stack Overflow

programmeradmin0浏览0评论

I am having issue with the mouseenter and the mouseleave event in javascript. The strange thing is that the code works if you substitute these 2 events with click or dblclick events. Hope you can help me here. PS: I'm using chrome. don't know how to make js work on fiddle... for now

here's the code:

/

(function() {
  window.onload = function() {
    var box = document.getElementsByClassName("box")[0];

    var change = function() {
      box.style.backgroundColor = "green";
    };

    var normal = function() {
      box.style.backgroundColor = "blue";
    }

    addEventListener("click", change, false);
    addEventListener("mouseleave", normal, false);
  };

}());

I am having issue with the mouseenter and the mouseleave event in javascript. The strange thing is that the code works if you substitute these 2 events with click or dblclick events. Hope you can help me here. PS: I'm using chrome. don't know how to make js work on fiddle... for now

here's the code:

https://jsfiddle/frempong69/t7du0kte/

(function() {
  window.onload = function() {
    var box = document.getElementsByClassName("box")[0];

    var change = function() {
      box.style.backgroundColor = "green";
    };

    var normal = function() {
      box.style.backgroundColor = "blue";
    }

    addEventListener("click", change, false);
    addEventListener("mouseleave", normal, false);
  };

}());
Share Improve this question edited Feb 2, 2016 at 17:51 twernt 20.6k5 gold badges34 silver badges41 bronze badges asked Feb 2, 2016 at 11:43 DrewDrew 1113 silver badges10 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

You are adding the mouseleave/mouseenter handlers to the window object. The click handler works because it bubbles to the window object, but the mouseenter and mouseleave events doesn't bubble so the listeners attached to the window object won't get triggered

You need add the listerns to the box element

(function() {
  window.onload = function() {
    var box = document.getElementsByClassName("box")[0];

    var change = function() {
      box.style.backgroundColor = "green";
    };

    var normal = function() {
      box.style.backgroundColor = "blue";
    }

    box.addEventListener("mouseenter", change, false);
    box.addEventListener("mouseleave", normal, false);
  };


}());
.box {
  background-color: red;
  width: 400px;
  height: 200px;
  margin: 50px auto;
  position: relative;
}
.box:after {
  content: " ";
  width: 0px;
  height: 0px;
  border-top: 100px solid transparent;
  border-right: 100px solid transparent;
  border-bottom: 100px solid transparent;
  border-left: 100px solid red;
  position: absolute;
  left: 100%;
  top: 50%;
  margin-top: -100px
}
<div class="box">
</div>

You can simply do like this

 box.onmouseenter = change;
 box.mouseleave = normal;

You must change

addEventListener("click", change, false);
addEventListener("mouseleave", normal, false);

with this

box.addEventListener("click", change, false);
box.addEventListener("mouseout", normal, false);

you just use this

<div class="box" onmouseover="style.background='green'" onmouseout="style.background='red'">
</div>

its work

发布评论

评论列表(0)

  1. 暂无评论