Why hover does not work in delegated event handlers?

前端 未结 1 362
借酒劲吻你
借酒劲吻你 2020-12-21 01:12

I am adding some elements dynamically and assigning a hover property to it in delegated event handlers for which I used below code and it did not work.

$(doc         


        
相关标签:
1条回答
  • 2020-12-21 01:36

    The function / event .hover is not actually an event, but just a shorthand for mouseenter and mouseleave. From the docs:

    The .hover() method binds handlers for both mouseenter and mouseleave events. You can use it to simply apply behavior to an element during the time the mouse is within the element.

    So you cannot use it to "delegate" events.

    Solution

    As you have already mentioned and as it is mentioned in the docs, you can use:

    $(static_parent).on("mouseenter mouseleave", element, function (e) {
      if (e.type == "mouseenter") {
        // check if it is mouseenter, do something
      } else {
        // if not, mouseleave, do something
      }
    });
    
    0 讨论(0)
提交回复
热议问题