Using delegate() with hover()?

前端 未结 2 1910
无人及你
无人及你 2020-12-16 01:25

I have a ul element with many li items:

  • ...

when the user hovers their mouse over an l

相关标签:
2条回答
  • 2020-12-16 01:49

    You can declare the handler function so that it pays attention to the event argument, and then check the type.

    0 讨论(0)
  • 2020-12-16 01:56

    You need to test for the type of event, like this:

    $("#myList").delegate("li", "hover", function ( event ) {
        if (event.type == 'mouseover') {
            showButtons();
        } else {
            hideButtons();
        }
    });
    

    Since there's only one handler to run for both events, we are checking to see which one fired, and running the appropriate code.

    As opposed to binding hover directly to the element where it is able to accept two handlers for the two event types.


    EDIT: Note that as of jQuery 1.4.3, the type of event that is reported when using 'hover' with .delegate() or .live() is no longer mouseover/mouseout (as it ought to be). Now it will be mouseenter/mouseleave, which just seems silly since they're non-bubbling events.

    So the if() statement would look like:

    if (event.type == 'mouseenter') {
        //...
    
    0 讨论(0)
提交回复
热议问题