jQuery: How to check if the mouse is over an element

前端 未结 6 2056
庸人自扰
庸人自扰 2021-01-11 13:16

I have a deferred function that binds a mouseenter event:

$(window).load(function(e) {
  var $container = $(\'.container\');
  $container.mouseenter(function         


        
6条回答
  •  春和景丽
    2021-01-11 13:34

    Mouseover event does exactly what you want.

    If the .conteiner does not exist in time you attach the mouseover event (is added dynamically in the future) you have to use .live method to attach the event.

    $(".conteiner").live("mouseover", function() {
       alert("hovered!");
    }) 
    

    It will work also when the mouse is already on the position, where the new element appears. Example here!

    For jQuery 1.7+ you should use .on method instead as the .live method is deprecated. Example here!

提交回复
热议问题