检查是否在Div区域内的onclick事件

时间:2019-06-06 13:58:05

标签: html onclick jscript

我有一个弹出菜单,当使用onmouseeneter将用户名悬停在用户名上时,在触发onmouseleave时菜单消失,出现

我的问题是,有时不触发mouseleave并保持菜单显示,这是可以的,但是我需要再次检查鼠标移动时div内是否存在div。以及单击事件(如果单击不在div之外,则关闭div)。

如何检查单击或鼠标移动是否在div范围内。

我没有运气就尝试了以下方法。尽管代码很好,但我需要另一种方法。

<div id='overlay' class='overlay' style='display:none;' 
onmouseover='showoverlay();' onmouseleave='removeoverlay();'> </div>


function showoverlay() {
    var overlay=document.getElementById("overlay"); 
    overlay.style.display="block";
    overlay.style.zIndex="999";     
    overlay.style.opacity="1";

}

function removeoverlay() {

    var overlay = document.getElementById("overlay");
    overlay.style.opacity="0";
    overlay.style.display="none"
    overlay.style.zIndex="-999";                

}


$(document).ready(function(){

    $(document).mouse(function(e)
    {
      var subject = $("#overlay"); 

        if(e.target.id != subject.attr('id') && 
        !subject.has(e.target).length)
       {
        removeoverlay();
       }

    });

});

1 个答案:

答案 0 :(得分:0)

您可以像这样使用is jQuery函数:

yarn cache clean