创建一个js的绑定事件,而这个需要一个addEventListener事件。在js里不要将事件写在标签里
<!DOCTYPE html><html><head> ?<style media="screen"> ?????#canvas{ ???????/*background-image: url(orangebgimage.jpg);*/ ???????border: 1px solid #001100; ?????} ?</style></head><body> ???<canvas id="canvas" width="300" height="300"></canvas></body><script type="text/javascript">// define global variablevar canvas = document.getElementById(‘canvas‘) ???// touch start listenerfunction touchStart(event) { ?/*除去预定方法*/ ????????event.preventDefault(); ????????alert(‘按下显示‘)} ???canvas.addEventListener("touchstart",touchStart,false);</script></html>
创建一个canvas对象,并且绑定对象。发现出现test.html:26 Uncaught TypeError: Cannot read property ‘addEventListener‘ of 的bug
这时注意将js代码写在body下面。这个问题主要是页面未全部加载成功导致的。在绑定默认事件时,需加入
event.preventDefault();
以确保原始方法不会被应用
js 绑定事件
原文地址:http://www.cnblogs.com/Amoswish/p/7594545.html