简体   繁体   中英

onmousedown over-rides onmouseup and onclick

Consider the following demo code:

<p id="demo" style="border: 1px solid black;width:300px;height:300px"></p>

<script>
  document.getElementById("demo").onmousedown = function(e){ alert('mousedown'); }
  document.getElementById("demo").onmouseup = function(e){ alert('mouseup'); }
  document.getElementById("demo").onclick = function(e){ alert('click'); }
</script>

Can someone please explain to me why neither the mouseup nor click events get fired?

When you press your mouse button down, you are triggering an alert which is blocking. Since alert is blocking, it isn't allowing your event handler to trigger once again when you release the click.

EDIT* - If you press your mouse down, and then press enter to quit the alert. When you release your mouse click you will see the mouse up event being triggered.

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