Javascript 如何使用 JQuery 检查光标是否悬停在元素上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4150358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:07:35  来源:igfitidea点击:

How can I check if the cursor is hovering on an element using JQuery

javascriptjquery

提问by Mark K

It possible to check if the cursor is hovering on an element.

可以检查光标是否悬停在元素上。

Something like

就像是

 $("#divId").is("hover");

NOTE: I just want to check not set event.

注意:我只想检查未设置事件。

回答by Anthony Corbelli

.is(':hover');

or

或者

$('#divId:hover');

回答by ?ime Vidas

Updated answer!

更新了答案!

$("#foo").hover(function() {
    $(this).data("hovered", true);
}, function() {
    $(this).data("hovered", false);
}); 

Testing if it is hovered...

测试它是否悬停...

if ( $("#foo").data("hovered") ) {
    // it is hovered
} else {
    // it's not hovered
}

回答by Peter Ajtai

You can use jQuery's hover(), mouseenter()or mouseover()

您可以使用 jQuery 的hover()mouseenter()或者mouseover()

$("#divId").hover(function() { alert("hovering"; });

This will fire on mouseenter and mouseleave. You can add separate event handlers for each.

这将在 mouseenter 和 mouseleave 上触发。您可以为每个添加单独的事件处理程序。

So if you want to do something like, if hovering over #divId increase x by one, and when you stop hovering decrease y by one:

因此,如果您想做类似的事情if hovering over #divId increase x by one, and when you stop hovering decrease y by one

$("#divId").hover(function() { ++x; }, 
                  function() { --y; });


If you really want an if hovering:

如果你真的想要一个if hovering

var hovering = 0;
$("#divId").hover(function() { hovering = 1; },
                  function() { hovering = 0; });
...
// Then inside somewhere useful. Maybe in a setInterval, or triggered by
//    another action...
if (hovering) { ...

Try it out with this jsFiddle

用这个 jsFiddle 试试

For example:

例如:

$(function() {
    var hovering = 0;
    $("div").hover(function() { hovering = 1; },
                   function() { hovering = 0; });

    $(document).keyup(function() {
         if (hovering) alert("hovering!");     // This is the "if hovering"
         else          alert("not hovering.");
    });
});

回答by Sam152

You can use .hover(). It's can be used like so:

您可以使用.hover()。它可以像这样使用:

$("selector").hover(
    function (){
        //mouse over
    },
    function (){
       //mouse out
    }
);

An example of it's use from the documentation is here:

文档中使用它的示例如下:

$("li").hover(
  function () {
    $(this).append($("<span> ***</span>"));
  }, 
  function () {
    $(this).find("span:last").remove();
  }
);

回答by James Black

Depending on what you are doing either mouseover()(http://api.jquery.com/mouseover/) or hover()(http://api.jquery.com/hover/) can be useful.

根据您在做什么mouseover()http://api.jquery.com/mouseover/)或hover()http://api.jquery.com/hover/)可能很有用。