Javascript OnMouseOver 没有属性

发布于 2024-12-16 10:42:28 字数 469 浏览 0 评论 0原文

我试图触发 javascript onMouseOver 事件,但我的每个锚点属性都没有 onMouseOver 属性。实际上,当用户将鼠标悬停在某个元素上时,我想显示一些其他锚点,但我什至无法收到工作警报。我不断收到以下错误:

Uncaught TypeError: Cannot call method 'getElementsByTagName' of null

这对我来说没有意义,因为如果我警告 getElementById() 我最终得到一个 DIV 元素,然后它应该获取该 div 元素内的所有锚标记...不知道我缺少什么。

JSFiddle 链接: http://jsfiddle.net/NfTtq/

另请注意,我正在执行此 Javascript 而不是 JQuery :S

I'm trying to trigger a javascript onMouseOver event without having the onMouseOver attribute on everyone of my anchor attributes. In reality I want to display some other anchors when the user mouses over an element, but I can't even get an alert to work. I keep getting the error of:

Uncaught TypeError: Cannot call method 'getElementsByTagName' of null

Which doesn't make sense to me because if I alert out the getElementById() I end up with a DIV element, which it should then get all the anchor tags inside that div element... Not sure what I am missing.

JSFiddle Link: http://jsfiddle.net/NfTtq/

Also note I am doing this Javascript not JQuery :S

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

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

发布评论

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

评论(4

追星践月 2024-12-23 10:42:28

您试图在元素存在之前找到该元素,您可以在 jsfiddle 中通过从左侧选择 onDomReady 而不是 no wrap (head) 来执行此操作

另一个问题是 onMouseOver,javascript 区分大小写,因此只有 onmouseover 可以工作。

这里是更新的jsfiddle:

http://jsfiddle.net/NfTtq/1/

You are trying to find the element before it exists, you can do this in jsfiddle by selecting onDomReady from the left instead of no wrap (head)

The other problem is onMouseOver, javascript is case sensitive so only onmouseover will work.

Here is updated jsfiddle:

http://jsfiddle.net/NfTtq/1/

神也荒唐 2024-12-23 10:42:28

您在加载 html 之前执行脚本,因此需要将其放入 window.onload

http://jsfiddle.net /NfTtq/5/

window.onload= function()
    {

      var node = document.getElementById('navFloat').getElementsByTagName('a');
      node[0].onmouseover = function()
        {
            alert('yes');
            document.getElementById('homeDrop').style.display = 'block';            
        }
}

You are executing script before html is loaded, so you need to put it into window.onload

http://jsfiddle.net/NfTtq/5/

window.onload= function()
    {

      var node = document.getElementById('navFloat').getElementsByTagName('a');
      node[0].onmouseover = function()
        {
            alert('yes');
            document.getElementById('homeDrop').style.display = 'block';            
        }
}
や莫失莫忘 2024-12-23 10:42:28

当在 DOM 上调用时,onMouseDown 事件仅以小型大写字母编写。这对我有用:

var node = document.getElementById('navFloat').getElementsByTagName('a');
node[0].onmouseover= function()
{
    document.getElementById('homeDrop').style.display = 'block';
}

The onMouseDown event is written in small caps only, when invoked on the DOM. This worked for me:

var node = document.getElementById('navFloat').getElementsByTagName('a');
node[0].onmouseover= function()
{
    document.getElementById('homeDrop').style.display = 'block';
}
素年丶 2024-12-23 10:42:28

这段代码适用于所有浏览器......

<p>
<a href="/">
<img alt="Ovdje uključiti " 
src="errorimages/normal.png" 
onmouseover="this.src='errorimages/hover.png'" 
onmousedown="this.src='errorimages/press.png'" 
onmouseup="this.src='errorimages/hover.png'" 
onmouseout="this.src='errorimages/normal.png'"
></a>
</p>

this code will work on all browsers.....

<p>
<a href="/">
<img alt="Ovdje uključiti " 
src="errorimages/normal.png" 
onmouseover="this.src='errorimages/hover.png'" 
onmousedown="this.src='errorimages/press.png'" 
onmouseup="this.src='errorimages/hover.png'" 
onmouseout="this.src='errorimages/normal.png'"
></a>
</p>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文