码迷,mamicode.com
首页 > Web开发 > 详细

js 绑定事件

时间:2017-09-26 01:01:48      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:order   ext   div   dia   url   type   doctype   function   can   

  创建一个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 variable

var canvas = document.getElementById(‘canvas‘)
    // touch start listener

function 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 绑定事件

标签:order   ext   div   dia   url   type   doctype   function   can   

原文地址:http://www.cnblogs.com/Amoswish/p/7594545.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!