How to detect mouseleave() on two elements at once?

后端 未结 5 1350
别跟我提以往
别跟我提以往 2020-12-06 11:15

Answer can be in vanilla js or jQuery. I want to hide a div with the id \"myDiv\" if the user is no longer hovering over a link with the id \"myLink\" or a span with the id

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-06 12:18

    Something like this should work:

    var count = 0;
    $('#myLink, #mySpan').mouseenter(function(){
        count++;
        $('#myDiv').show();
    }).mouseleave(function(){
        count--;
        if (!count) {
            $('#myDiv').hide();
        }
    });
    

    jsfiddle

提交回复
热议问题