如何使用JQuery/Javascript检查光标是否位于html页面上的div
上?
我正在尝试获取光标坐标,以查看它们是否在元素的矩形中。也许有预定义的方法?
更新,不要说任何关于hover
事件等。我需要一些方法,它将返回真/假的一些元素在页面,如:
var result = underElement('#someDiv'); // true/false
发布于 2010-08-03 19:03:19
我不太确定为什么您会如此急切地避免悬停:考虑以下脚本
$(function(){
$('*').hover(function(){
$(this).data('hover',1); //store in that element that the mouse is over it
},
function(){
$(this).data('hover',0); //store in that element that the mouse is no longer over it
});
window.isHovering = function (selector) {
return $(selector).data('hover')?true:false; //check element for hover property
}
});
基本上的想法是,您使用鼠标悬停来在元素上设置一个标志,表明鼠标在它上面/不再在它上面。然后编写一个检查该标志的函数。
发布于 2010-08-03 19:06:39
最简单的方法可能是跟踪鼠标始终停留在哪个元素上。尝试如下所示:
<div id="1" style="border:solid 1px red; width:50px; height:50px;"></div>
<div id="2" style="border:solid 1px blue; width:50px; height:50px;"></div>
<div id="3" style="border:solid 1px green; width:50px; height:50px;"></div>
<input type="hidden" id="mouseTracker" />
$(document).ready(function() {
$('*').hover(function() {
$('#mouseTracker').val(this.id);
});
});
然后你的函数就是
function mouseIsOverElement(elemId) {
return elemId === $('#mouseTracker').val();
}
发布于 2011-06-29 01:17:01
你不能勾选$(Select)hover(‘:.is’)吗?
https://stackoverflow.com/questions/3395293
复制相似问题