onMouseOut 在锚标记内移动时频繁触发

发布于 2024-11-30 16:11:32 字数 1081 浏览 2 评论 0原文

我有一个锚标记,它同时具有 onMouseOver 和 onMouseOut 事件。目的是显示工具提示。当我将鼠标悬停在锚标记上时,会适当触发 onMouseOver 事件并显示工具提示,但也会触发 onMouseOut 事件并隐藏工具提示。当我将鼠标移到锚链接上时,这两个事件都会非常频繁地触发,本质上使工具提示快速可见和不可见。我希望 onMouseOut 仅在我位于锚标记边界之外时触发。

我认为相关的代码:

HTML:

<ul>
<li>
<a id="1" href="event.php?1" onmouseover="tooltip(this.offsetWidth, this.offsetHeight, this.childNodes, 1)" onmouseout="tooltipHide(1)">Text</a>
<div class="tt">
<div id="tt2" class="tooltip">
...
</div>
</div>
</li>

<li>
<a id="2" href="event.php?2" onmouseover="tooltip(this.offsetWidth, this.offsetHeight, this.childNodes, 2)" onmouseout="tooltipHide(2)">Text</a>
<div class="tt">
<div id="tt2" class="tooltip">
...
</div>
</div>
</li>
</ul>

Javascript:

function tooltipHide(id) {
tt = document.getElementById("tt"+id);
tt.style.display = "none";
}

function tooltip(oW, oH, e, id) {
...
tt = document.getElementById("tt"+id);
tt.style.display = "block";
...
}

I have an anchor tag which has both a onMouseOver and onMouseOut event. The purpose is to display a tooltip. When I hover over the anchor tag, the onMouseOver event is appropriately triggered and the tooltip shows, but the onMouseOut event is also triggered and the tooltip is hidden. As I move the mouse over the anchor link, both events fire very frequently essentially making the tooltip flash visible and invisible very quickly. I want the onMouseOut to only fire once I'm outside the boundary of the anchor tag.

Code which I think is relevant:

HTML:

<ul>
<li>
<a id="1" href="event.php?1" onmouseover="tooltip(this.offsetWidth, this.offsetHeight, this.childNodes, 1)" onmouseout="tooltipHide(1)">Text</a>
<div class="tt">
<div id="tt2" class="tooltip">
...
</div>
</div>
</li>

<li>
<a id="2" href="event.php?2" onmouseover="tooltip(this.offsetWidth, this.offsetHeight, this.childNodes, 2)" onmouseout="tooltipHide(2)">Text</a>
<div class="tt">
<div id="tt2" class="tooltip">
...
</div>
</div>
</li>
</ul>

Javascript:

function tooltipHide(id) {
tt = document.getElementById("tt"+id);
tt.style.display = "none";
}

function tooltip(oW, oH, e, id) {
...
tt = document.getElementById("tt"+id);
tt.style.display = "block";
...
}

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

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

发布评论

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

评论(2

夜访吸血鬼 2024-12-07 16:11:32

只要鼠标进入或移动到被覆盖的项目上,就会触发 onmouseover。您可能希望 onmouseenter 显示工具提示。仅当鼠标穿过元素的边界进入内部时才会触发。

onmouseover is triggered anytime the mouse enters OR moves over the item being overed. You'd probably want onmouseenter show the tooltip, instead. That's triggered only when the mouse crosses the element's borders into the interior.

奢欲 2024-12-07 16:11:32

有点疯狂,但问题是当工具提示出现时,它与触发事件的文本重叠。由于某种原因,这导致这两个函数快速来回触发。我通过将工具提示移离文本解决了这个问题。

A little crazy but the issue was that the when the tooltip appeared, it overlapped with the text which triggered the event. For some reason, this caused both functions to trigger back and forth quickly. I fixed the problem by moving the tooltip away from the text.

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