检查光标下是否存在元素

时间:2022-01-19 01:53:22

I want to use javascript to detect if an element exists under the cursor of the mouse, meaning that for example I want to check if I was on an "img" or if i was on a "p" element.

我想使用javascript来检测鼠标光标下是否存在元素,这意味着我想检查我是否在“img”上,或者我是否在“p”元素上。

I don't want to add a hover method, the application does dragging and dropping of elements, and while dragging, I want to deny the dropping of that element in an area where another element exists.

我不想添加悬停方法,应用程序会拖放元素,拖动时,我想拒绝在另一个元素存在的区域中删除该元素。

Is it possible using javascript/jQuery and html ? and what would be right way to do it ?

是否可以使用javascript / jQuery和html?什么是正确的方法呢?

2 个解决方案

#1


1  

You can try it:

你可以尝试一下:

window.onmousemove = function(e){
    var e = e || event;
    var all = document.all;
    for(var i=0;i<all.length;i++)
        if(e.x>=all[i].offsetLeft && e.x<=(all[i].offsetLeft+all[i].offsetWidth) && e.y>=all[i].offsetTop && e.y<=(all[i].offsetTop+all[i].offsetHeight)){
            //Do something
            break;
        }
}

#2


0  

Everything you are asking can be set in the options of jQueryUI draggable and droppable interaction methods. If the current draggable system isn't set up for simple user options coding, take a look at these.

你问的一切都可以在jQueryUI draggable和droppable交互方法的选项中设置。如果当前可拖动系统未设置为简单的用户选项编码,请查看这些。

There are events built in like the "over" you mention. You can set droppables to only accept certain elements using simple jQuery selectors or callbacks. The API is robust and there is great support for it

有一些事件像你提到的“结束”一样。您可以将droppables设置为仅使用简单的jQuery选择器或回调接受某些元素。 API非常强大,并且对它有很大的支持

http://jqueryui.com/demos/draggable/

#1


1  

You can try it:

你可以尝试一下:

window.onmousemove = function(e){
    var e = e || event;
    var all = document.all;
    for(var i=0;i<all.length;i++)
        if(e.x>=all[i].offsetLeft && e.x<=(all[i].offsetLeft+all[i].offsetWidth) && e.y>=all[i].offsetTop && e.y<=(all[i].offsetTop+all[i].offsetHeight)){
            //Do something
            break;
        }
}

#2


0  

Everything you are asking can be set in the options of jQueryUI draggable and droppable interaction methods. If the current draggable system isn't set up for simple user options coding, take a look at these.

你问的一切都可以在jQueryUI draggable和droppable交互方法的选项中设置。如果当前可拖动系统未设置为简单的用户选项编码,请查看这些。

There are events built in like the "over" you mention. You can set droppables to only accept certain elements using simple jQuery selectors or callbacks. The API is robust and there is great support for it

有一些事件像你提到的“结束”一样。您可以将droppables设置为仅使用简单的jQuery选择器或回调接受某些元素。 API非常强大,并且对它有很大的支持

http://jqueryui.com/demos/draggable/