JQuery判断鼠标是否移入或者移出某个元素,可以使用mouseenter和mouseleave事件来判断鼠标是否在某个元素上。 以下是一个简单的例子: - <div id="myElement">Hover over me!</div>
复制代码- $(document).ready(function(){
- var isMouSEOver = false;
-
- $('#myElement').mouseenter(function(){
- isMouSEOver = true;
- console.log('Mouse is over the element.');
- }).mouseleave(function(){
- isMouseOver = false;
- console.log('Mouse is not over the element anymore.');
- });
-
- // 检查鼠标是否在元素上
- function isMouseOverElement() {
- return isMouseOver;
- }
-
- // 使用方法
- if (isMouseOverElement()) {
- console.log('Mouse is currently over the element.');
- } else {
- console.log('Mouse is not over the element.');
- }
- });
复制代码
|