cannot use .bind() to bind hover

前端 未结 2 359
执笔经年
执笔经年 2021-02-03 10:28

I experimenting with jQuery. As I was trying I found out that I can\'t use hover event with .bind. And I don\'t know what is wrong.

$(document).ready(function()         


        
相关标签:
2条回答
  • 2021-02-03 11:00

    You need to use the mouseenter and mouseleave events (which .hover() uses) directly when binding with an object like this:

    $(document).ready(function(){
     $('.some-class').bind({
      mouseenter: function(e) {
      // Hover event handler
       alert("hover");
      },
      mouseleave: function(e) {
      // Hover event handler
       alert("hover");
      },
      click: function(e) {
      // Click event handler
       alert("click");
      },
      blur: function(e) {
      // Blur event handler
      }
     });
    });
    

    .hover() is defined specially here in the jQuery event code...it simply isn't supported like other events in places like .bind(), since it's not an event, it's just a function to help you bind the mouseenter and mouseleave events.

    0 讨论(0)
  • 2021-02-03 11:09

    Not much of a why, but it is simply not in the spec. Check the doc - hover is not listed in the bindable events.

    http://api.jquery.com/bind/

    0 讨论(0)
提交回复
热议问题