jQuery: trigger a hover event from another element

后端 未结 4 1154
我在风中等你
我在风中等你 2020-11-30 05:37

When you hover over one

4条回答
  •  臣服心动
    2020-11-30 06:20

    Try this:

    $('.initiator').on('mouseenter mouseleave', function(e) {
        $('.receiver').trigger(e.type);
    })
    

    It will apply the same triggers for the receiver as the initiator receives for both mouseenter and mouseleave. Note that:

    .hover(over, out) 
    

    is just a high-level variant of:

    .on('mouseenter', over).on('mouseleave', out)
    

    so using that information you can be more precise when binding and triggering mouse events.

    As noted in the comment, you can also use:

    $('.initiator').hover(function(e) {
        $('.receiver').trigger(e.type);
    })
    

    There are lots more to read here: http://api.jquery.com/hover/

提交回复
热议问题