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.
注意:我只想检查未设置事件。
5 个解决方案
#1
22
.is(':hover');
or
要么
$('#divId:hover');
#2
4
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
}
#3
3
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
:
因此,如果您想要执行类似操作,如果将鼠标悬停在#divId上,请将x增加1,当您停止将鼠标悬停时减少y:
$("#divId").hover(function() { ++x; },
function() { --y; });
If you really want an 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
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.");
});
});
#4
2
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();
}
);
#5
0
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/)可能很有用。
#1
22
.is(':hover');
or
要么
$('#divId:hover');
#2
4
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
}
#3
3
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
:
因此,如果您想要执行类似操作,如果将鼠标悬停在#divId上,请将x增加1,当您停止将鼠标悬停时减少y:
$("#divId").hover(function() { ++x; },
function() { --y; });
If you really want an 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
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.");
});
});
#4
2
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();
}
);
#5
0
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/)可能很有用。