onmouseover 和超链接悬停

发布于 2024-10-03 04:17:24 字数 1060 浏览 13 评论 0 原文

我有一个带有链接的 div,当您将鼠标悬停在链接上时,它们会带有下划线。我向 div 添加了一个 onmouseover JS 事件,现在当我将鼠标悬停在超链接上时,超链接不再带有下划线,而是执行我放入 onmouseover 事件中的任何操作。

函数代码:

function addPlus(elementId)
{
 if(typeof addPlus.backup == 'undefined')
  addPlus.backup = document.getElementById(elementId).innerHTML;
 if(full)
 {
  document.getElementById(elementId).innerHTML = plusCode + addPlus.backup;
  return backup;
 }
 else
  return snippet;
}

div 代码:

<div class="nav_bar" id="navbar1" onmouseover="addPlus('navbar1')" onmouseout="removePlus('navbar1')">

编辑:我尝试过 return true;return Boolean(true);,并return new Boolean(true);,试图按照 Chad 的建议“返回 true”。它们都不起作用。抱歉,我真的不知道该怎么办;我是 JavaScript 新手。

编辑2:该死的,我刚刚意识到Chad意味着我在div标签中返回true。所以现在我有

I had a div with links that underlined once you hover over them. I added an onmouseover JS event to the div and now the hyperlinks no longer underline when I hover over them, but instead whatever action I put into the onmouseover event gets executed instead.

CODE from function:

function addPlus(elementId)
{
 if(typeof addPlus.backup == 'undefined')
  addPlus.backup = document.getElementById(elementId).innerHTML;
 if(full)
 {
  document.getElementById(elementId).innerHTML = plusCode + addPlus.backup;
  return backup;
 }
 else
  return snippet;
}

div code:

<div class="nav_bar" id="navbar1" onmouseover="addPlus('navbar1')" onmouseout="removePlus('navbar1')">

EDIT: I've tried return true;, return Boolean(true);, and return new Boolean(true);, in an attempt to "return true" as Chad suggested. None of them work. Sorry, I really don't know what to do; I'm new to Javascript.

EDIT 2: Darn it, I just realized Chad meant that I return true in the div tag. So now I have <div class="nav_bar" id="navbar1" onmouseover="addPlus('navbar1');return true" onmouseout="removePlus('navbar1')">, but it unfortunately still doesn't work.

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

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

发布评论

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

评论(3

番薯 2024-10-10 04:17:25

为了使事件传播,您需要从 onmouseover 处理程序返回 true。

for the event to propagate you need to return true from your onmouseover handler.

请恋爱 2024-10-10 04:17:25
  1. 如果您使用 CSS 而不是 JavaScript 为链接添加下划线,则不会发生这种情况
  2. 您可能有一些烂代码,在 HTML 或 JavaScript 中显式设置 onmouseover 处理程序来处理链接下划线(而不是正确使用addEventListener),然后您将覆盖此处理程序。如果没有更多信息,很难猜测可能出了什么问题。提供代码示例以获得更多帮助。
  1. This won't occur if you use CSS to underline your links instead of JavaScript
  2. You presumably have some rotten code that is explicitly setting an onmouseover handler in HTML or JavaScript to handle the link underline (instead of properly using addEventListener), and then you are overwriting this handler. It's hard to guess what might be wrong without more information. Supply a code example for more help.
≈。彩虹 2024-10-10 04:17:24

If you don't want for js event to fire you should stop event propogation when mouse enters <a>. Try adding onmouseover event handler for each hyperlink with return false - <a onmouseover="return false" ... - that should help.

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