简体   繁体   中英

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

 <!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. Instead store the object reference and set its innerHTML property.

 <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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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