简体   繁体   中英

checking code of Javascript “on mousedown”

 <script> function lighton() { document.getElementById('myimage').src = "images\\download.png"; } function lightoff() { document.getElementById('myimage').src = "images\\link.png"; } </script> 
 <!DOCTYPE html> <html> <head> <script> function lighton() { document.getElementById('myimage').src = "images\\download.png"; } function lightoff() { document.getElementById('myimage').src = "images\\link.png"; } </script> </head> <body> <img id="myimage" onmousedown="lighton()" onmouseup="lightoff()" src="images\\link.png" width="100" height="180" /> <p>Click mouse and hold down!</p> </body> </html> 

see why this is not working when i click the mouse button first image disappears and second image does not open

I attached the event handers this way, and it started working. Remember to do this after "onload".

var el = document.getElementById("myimage");
el.addEventListener("mousedown",lightoff);
el.addEventListener("mouseup",lighton);

The error I was seeing with your code was that it could not find the functions.

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