2013-03-15 24 views
0

我的网页上有我的圈子,编号为circle。现在,我想要的是当鼠标悬停在圆上时,应该显示一条消息。 这是我在JavaScript文件中的代码:使用onmouseover调用函数

document.getElementById("circle").onmouseover=function(){ 
    var information = document.getElementById("toast"); 
    message = "hello"; 
    if (alert == null){ 
      var toastHTML = '<div id="toast">' + message + '</div>'; 
      document.body.insertAdjacentHTML('beforeEnd', toastHTML); 
     } 
    else 
     information.style.opacity = 0.9; 
    intervalCounter = setInterval("hideToast",1000); 
}; 

但似乎有一定的误差,javascript的终端给出:

Uncaught TypeError: Cannot set property 'onmouseover' of null 
+0

'cicrle '应该是'圈子'?或那是一个错字? – 2013-03-15 18:52:52

+0

在代码的第一行更正“circle”的拼写。此外,我不确定这将捕获具有相同ID的多个元素。也许把类放在他们身上,并使用jQuery的类选择器来获得圈子? – jonhopkins 2013-03-15 18:53:00

+1

你可以显示HTML吗?乍一看,document.getElementById('circle')没有找到元素。 – 2013-03-15 18:53:13

回答

1

总结你的代码onload事件像

window.onload=function(){ 
    document.getElementById("cicrle").onmouseover=function(){ 
     // code goes here 
    }; 
};