JavaScript:onMouseOver 事件无法与其他事件一起正常工作

发布于 2024-10-18 08:11:11 字数 583 浏览 5 评论 0原文

我有一个包含 15x15 表格的 HTML 网页,还有一个小方形 div,当您在 div 上按住鼠标左键时,它会跟随鼠标。

我已将 onmouseover 事件分配给 15x15 表格,以便当鼠标悬停在单元格上时,名为“gridPlacement”的变量将设置鼠标悬停在其上的表格单元格的唯一 id 值。

onmouseover 事件似乎工作正常,当您将鼠标悬停在单元格上时,变量“gridPlacement”就会立即设置为单元格的 id。

但是,当触发 onmousedown 和 onmousemove 事件时(当鼠标在我前面提到的 div 上按住鼠标左键时),有时当您将鼠标悬停在单元格上时,不会设置变量“gridPlacement”,有时您必须设置将鼠标移到单元格上几次即可使其工作。

看来 onmousedown 和 onmousemove 事件似乎对我的 15x15 桌子上的 onhover 事件有影响。

有谁知道为什么会发生这种情况以及我如何解决这个问题?

我希望你理解我,我已尽力解释我的问题。

问题已解决!

更改用户拖动的元素的偏移量就可以了,谢谢大家帮助我! :)

I have a HTML webpage that contains a 15x15 table and I also have a small square div that follows the mouse when you press and hold the left mouse button on the div.

I have assigned an onmouseover event to the 15x15 table so that when the mouse hovers over a cell, a variable called "gridPlacement" is set the value of the unique id of the table cell the mouse hovered over.

The onmouseover event seems to work fine and instantly as soon as you hover over a cell the variable "gridPlacement" is set to the cell's id.

But when the onmousedown and onmousemove events are triggered (when the mouse presses and holds the left mouse button on the div I mentioned earlier), sometimes when you hover over a cell the variable "gridPlacement" isn't set, and sometimes you have to move your mouse over the cell a few times for it to work.

It seems the onmousedown and onmousemove events seem to have an impact on the onhover event on my 15x15 table.

Does anybody know why this happens and how I can work around this problem?

I hope you understand me, I've tried to explain my problem as best as I can.

PROBLEM SOLVED!

Changing the offset of the element the user is dragging did the trick, thank you all for helping me! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转角预定愛 2024-10-25 08:11:11

我认为佐佐是正确的。我遇到了类似的问题,我使用的解决方法是更改​​鼠标后面的元素的偏移量,以便它不会直接出现在鼠标下方(可能是侧面/上方/下方的几个像素或其他)。

希望这有帮助。

I think zozo is correct. I ran into a similar problem and the workaround I used is to change the offset of the element following the mouse so that it does not appear directly under the mouse (maybe a few pixels to the side/above/below or something).

Hope this helps.

可遇━不可求 2024-10-25 08:11:11

您所问的问题的发生是因为当您按下按钮时,div 位于鼠标下方,因此您实际上并未将单元格悬停在单元格上(至少这是我从您的描述中得到的)。解决方法是在退出某个单元格(onmouseout)时设置所需的变量,但这对您没有帮助,因为我想您想知道您已经结束的女巫单元格,所以我想您可以获取鼠标位置并进行比较到细胞的位置......除非你得到更好的答案,因为这有点......不那么容易。

The problem you asked about happens because when you have the button pressed the div is under your mouse, so you don't actually hover the cell (at least thats what I get from your description). A workaround would be to set the desired variable when you exit a certain cell (onmouseout) but this does not help you since I guess you want to know over witch cell you are over, so I guess you can get the mouse position and compare it to the position of the cells... unless you get a better answer because this is kind of... not so easy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文