Jquery 条件检查是(':悬停')不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8010267/
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
Jquery condition check is(':hover') not working
提问by Wasim A.
$('.xx').mouseenter(function(){
if($(this).is(':hover'))
alert('d');
else
alert('f');
});
Here is my code, it should alert 'd' but every time it alerts 'f' What's error Here
这是我的代码,它应该提醒 'd' 但每次它提醒 'f' 这是什么错误
回答by mathheadinclouds
function idIsHovered(id){
return $("#" + id + ":hover").length > 0;
}
回答by Frédéric Hamidi
回答by Connell
As Frederic said, :hoveris part of CSS and is not a selector in jQuery.
正如 Frederic 所说,:hover是 CSS 的一部分,而不是 jQuery 中的选择器。
For an alternative solution, read How do I check if the mouse is over an element in jQuery?
对于替代解决方案,请阅读如何检查鼠标是否位于 jQuery 中的元素上?
Set a timeout on the mouseout to fadeout and store the return value to data in the object. Then onmouseover, cancel the timeout if there is a value in the data.
Remove the data on callback of the fadeout.
将鼠标移出超时设置为淡出并将返回值存储到对象中的数据。然后onmouseover,如果数据中有值就取消超时。
删除淡出回调的数据。
回答by rogerlsmith
Try something like this-
尝试这样的事情 -
$('.xx').hover(function(){
alert('d');
}, function() {
alert('f);
});
回答by Evan
why dont you just use .hover?
你为什么不只使用.hover?
$(".xx").hover(function(){
alert("d");
});
回答by FRAGnat
Try something like this
尝试这样的事情
flag = ($('.xx:hover').length>0);
So you can find out if the mouse is, the object
这样你就可以找出鼠标是否是对象
回答by l00k
x.filter(':hover').length
This may be also usable when you already had queried some objects / or inside callback function.
当您已经查询了某些对象/或内部回调函数时,这也可能有用。
回答by Legends
Here is a little jQuery pluginthat checks if the mouse is over an element.
这是一个小jQuery 插件,用于检查鼠标是否位于元素上。
Usage:
用法:
$("#YourElement").isMouseOverMe();
$("#YourElement").isMouseOverMe();
Example:
例子:
(function($) {
var mx = 0;
var my = 0;
$(document).mousemove(function(e) { // no expensive logic here
mx = e.clientX;
my = e.clientY;
})
$.fn.isMouseOverMe = function() {
var $el = $(this);
var el_xmin = $el.offset().left;
var el_ymin = $el.offset().top;
var el_xmax = el_xmin + $el.width();
var el_ymax = el_ymin + $el.height();
return mx >= el_xmin && mx <= el_xmax && my >= el_ymin && my <= el_ymax;
};
}(jQuery));
$(document).mouseup(function(e) {
console.log($("#div").isMouseOverMe())
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>Click inside or outside of the yellow box</h2>
<div id="div" style="width:200px;height:200px;background-color:yellow;margin-top:50px"></div>