如何将事件附加到 HTML5 Canvas?
所以我知道您可以直接执行以下操作:
<canvas id="someCanvas" onmousedown="blah();">
<script>
function blah() {
dosomething();
}
</script>
How will I Attach the event only in code?
我尝试这样做:
var canvas = document.getElementById("someCanvas");
canvas.onmousedown = function() {
doSomething();
}
但它不起作用,并且 doSomething 没有被调用...我做错了什么?
So I know you can directly do:
<canvas id="someCanvas" onmousedown="blah();">
<script>
function blah() {
dosomething();
}
</script>
How would I attach the event in code only?
I tried doing:
var canvas = document.getElementById("someCanvas");
canvas.onmousedown = function() {
doSomething();
}
But it does not work and doSomething does not get called... what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 AddEventListener,如下所示:
https://developer.mozilla.org/en/DOM/element.addEventListener
Try using AddEventListener, as such:
https://developer.mozilla.org/en/DOM/element.addEventListener