开发者

name of element mouse is hovering

I'm using a global variable that toggles between true and false on mouse enter of a div and then I'm using the value of this variable elsewhere to show / hide another element depending on the value.

Actually, another way to do what I want would be to know if the mouse is hovering a div called MyDiv. I'm looking to remove the use of the global variable that toggles with the mouseenter/mouseleave events.

I tried this:

 var test = $('#MyDiv').mouseover() ? 1 : 0;

but it's not working.

Let me know if you have a 1-liner suggestion for returning the name of the div that's being hovered.

Thanks.

PS: I already k开发者_运维问答now it can be done with more than 1 line


You should store your flag in jQuery's data storage:

$(this).data("mouseover", true);
if ($(this).data("mouseover"))


You can do this:

$('#MyDiv').mouseover(function(){
    //show/hide other div or any other code
}) ;

OR

you can set another global variable that tells you whether mouse is hovering on MyDiv like this:

$('#MyDiv').mouseover(function(){
    myDivHovering = true;
}).mouseout(function() {
    myDivHovering = false;
}) ;


jQuery uses a callback style to handle events. The functionality you're describing can be implemented like so:

var mouse_on_mydiv = false;
$('#MyDiv').hover(function onMouseIn() {
    mouse_on_mydiv = true;
    alert(this.id); // => 'MyDiv'
}, function onMouseOut() {
    mouse_on_mydiv = false;
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜