简体   繁体   English

鼠标事件,我的代码有什么问题

[英]mouse events, whats wrong in my code

In the html part I don't want to include the events related code, events related code should be in inside the script tag在html部分我不想包含事件相关代码,事件相关代码应该在脚本标签内

 <!doctype html> <head> <style> div{ width:200px; background-color:grey; } </style> <body> <p>use the below area for events</p> <div> point here </div> <a id="event_output"></a> <script> var output=document.getElementById("event_output").innerHTML; var div=document.getElementsByTagName("div"); div[0].onmouseover=function(){output="mouse over"} div[0].onmouseout=function(){output="mouse out"} </script> </body>

You are just updating the output variable which is a string.您只是在更新作为字符串的output变量。 Instead store the object reference and set its innerHTML property.而是存储对象引用并设置其innerHTML属性。

 <style> div { width: 200px; background-color: grey; } </style> <p>use the below area for events</p> <div>point here</div> <a id="event_output"></a> <script> var output = document.getElementById("event_output"); var div = document.getElementsByTagName("div"); div[0].onmouseover = function() { output.innerHTML = "mouse over" } div[0].onmouseout = function() { output.innerHTML = "mouse out" } </script>

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

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