onmousedown over-rides onmouseup and onclick

北慕城南 提交于 2019-12-11 20:02:29

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/27161793/onmousedown-over-rides-onmouseup-and-onclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!