Is it possible to use jQuery .on and hover?

前端 未结 10 928
失恋的感觉
失恋的感觉 2020-11-22 11:03

I have a

    that is populated with javascript after the initial page load. I\'m currently using .bind with mouseover and
10条回答
  •  时光说笑
    2020-11-22 11:50

    You can you use .on() with hover by doing what the Additional Notes section says:

    Although strongly discouraged for new code, you may see the pseudo-event-name "hover" used as a shorthand for the string "mouseenter mouseleave". It attaches a single event handler for those two events, and the handler must examine event.type to determine whether the event is mouseenter or mouseleave. Do not confuse the "hover" pseudo-event-name with the .hover() method, which accepts one or two functions.

    That would be to do the following:

    $("#foo").on("hover", function(e) {
    
        if (e.type === "mouseenter") { console.log("enter"); }
        else if (e.type === "mouseleave") { console.log("leave"); }
    
    });
    

    EDIT (note for jQuery 1.8+ users):

    Deprecated in jQuery 1.8, removed in 1.9: The name "hover" used as a shorthand for the string "mouseenter mouseleave". It attaches a single event handler for those two events, and the handler must examine event.type to determine whether the event is mouseenter or mouseleave. Do not confuse the "hover" pseudo-event-name with the .hover() method, which accepts one or two functions.

提交回复
热议问题