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?
Try using AddEventListener, as such:
canvas.addEventListener("mousedown", function() {} );
https://developer.mozilla.org/en/DOM/element.addEventListener
精彩评论