如何检查鼠标是否在元素上?
我将元素移动到光标位置,我需要检查鼠标是否在另一个元素上。该怎么做呢?
<div class="dragged"></div> // I can move it
<div class="dropped"></div> // Need to drop here$('.dragged').mousemove(function(e) {
...
if($(".dropped").is(":hover")) { // of course doesn't work
...
}
});提前谢谢。
发布于 2014-07-29 17:25:12
您可以这样尝试:
$('#test').click(function() {
if ($('#Test').is(':hover')) {
alert('Test');
}
});发布于 2014-07-29 17:27:43
一种有效的方法是在鼠标按enter时“标记”.dropped标记元素。最后,当您移动.dragged时,您可以检查.dropped是否具有您放入其中的标记。
$('.dropped').hover(function() {
$(this).addClass('hovered');
}, function() {
$(this).removeClass('hovered');
});
$('.dragged').mousemove(function(e) {
...
if($(".dropped").hasClass(".hovered")) {
...
}
});致以问候。
发布于 2014-07-29 17:33:29
试试这个:
var hovred=null;
$('.dropped').mouseenter(function(e) {
hovred=$(this);
});
$('.dropped').mouseleave(function(e) {
hovred=null;
});
$('.dropped').mousemove(function(e) {
...
if($(this)==hovred && hovred != null) {
//do your stuff here man
}
});https://stackoverflow.com/questions/25011915
复制相似问题