gpt4 book ai didi

javascript - 检查鼠标是否在 div 内

转载 作者:可可西里 更新时间:2023-11-01 02:53:29 24 4
gpt4 key购买 nike

我想使用 if 语句来检查鼠标是否在某个 div 内,如下所示:

if ( mouse is inside #element ) {
// do something
} else {
return;
}

这将导致函数在鼠标在#element 内部时启动,在鼠标在#element 外部时停止。

最佳答案

您可以注册 jQuery 处理程序:

var isOnDiv = false;
$(yourDiv).mouseenter(function(){isOnDiv=true;});
$(yourDiv).mouseleave(function(){isOnDiv=false;});

没有 jQuery 替代品:

document.getElementById("element").addEventListener("mouseenter", function(  ) {isOnDiv=true;});
document.getElementById("element").addEventListener("mouseout", function( ) {isOnDiv=false;});

和其他地方:

if ( isOnDiv===true ) {
// do something
} else {
return;
}

关于javascript - 检查鼠标是否在 div 内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36767196/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com