可以检查光标是否悬停在元素上。
类似这样的代码:
$("#divId").is("hover");
注意:我只是想检查事件是否设置。
可以检查光标是否悬停在元素上。
类似这样的代码:
$("#divId").is("hover");
注意:我只是想检查事件是否设置。
.is(':hover');
或者$('#divId:hover');
更新的答案!
(此处无需翻译)$("#foo").hover(function() {
$(this).data("hovered", true);
}, function() {
$(this).data("hovered", false);
});
测试是否悬停...
if ( $("#foo").data("hovered") ) {
// it is hovered
} else {
// it's not hovered
}
hover=yes
,并在mouseleave时存储hover=no
?这样无论悬停多长时间都可以工作。 - Peter Ajtaihover()
, mouseenter()
或者 mouseover()
方法。$("#divId").hover(function() { alert("hovering"; });
如果悬停在#divId上,则将x增加1,并在停止悬停时将y减少1
:$("#divId").hover(function() { ++x; },
function() { --y; });
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) { ...
例如:
$(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.");
});
});
if ( hovering? ) { do one thing; } else { do another thing; }
- Šime Vidas您可以使用.hover()。它可以这样使用:
$("selector").hover(
function (){
//mouse over
},
function (){
//mouse out
}
);
$("li").hover(
function () {
$(this).append($("<span> ***</span>"));
},
function () {
$(this).find("span:last").remove();
}
);
根据你的需求,mouseover()
(http://api.jquery.com/mouseover/) 或者 hover()
(http://api.jquery.com/hover/) 都可能会有用。
:hover
作为初始选择器并不起作用。在任何浏览器中都不能使用is(":hover")
。在撰写本文时,使用is("hover")
在所有浏览器中都能起作用。 - BenCr