简体   繁体   English

没有属性的Javascript OnMouseOver

[英]Javascript OnMouseOver without attribute

I'm trying to trigger a javascript onMouseOver event without having the onMouseOver attribute on everyone of my anchor attributes. 我正在尝试触发javascript onMouseOver事件,而我的每个锚点属性都没有onMouseOver属性。 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. 这对我来说没有意义,因为如果我警告getElementById()我最终将得到一个DIV元素,然后该元素应将所有锚标签放入该div元素中。。。不确定我缺少什么。

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

Also note I am doing this Javascript not JQuery :S 另请注意,我正在执行此Javascript,而不是JQuery:S

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) 您试图在元素存在之前就找到它,可以在jsfiddle中通过从左侧选择onDomReady而不是no wrap (head)

The other problem is onMouseOver , javascript is case sensitive so only onmouseover will work. 另一个问题是onMouseOver ,JavaScript区分大小写,因此只有onmouseover可以工作。

Here is updated jsfiddle: 这是更新的jsfiddle:

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

You are executing script before html is loaded, so you need to put it into window.onload 您正在加载html之前执行脚本,因此需要将其放入window.onload

http://jsfiddle.net/NfTtq/5/ 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';            
        }
}

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>

The onMouseDown event is written in small caps only, when invoked on the DOM. 当在DOM上调用onMouseDown事件时,仅将其写成小写。 This worked for me: 这为我工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM