javascript为组件添加事件

Html5中中使用JS为组件添加事件时,需要使用一个对象来持有事件监听,然后进行添加和移除事件监听

var downhandler = function(e) {//持有对象
				cnvs_down(e);
			}
			function cnvs_down(e) {
				x = e.clientX;
				y = e.clientY;
				draw(x, y);
				startX = e.clientX;
				startY = e.clientY;
			}

			var uphandler = function(e) {
				cnvs_up(e);
			}
			function cnvs_up(e) {
				x = startX;
				y = startY;
				x1 = e.clientX;
				y1 = e.clientY;
				var c = document.getElementById("myCanvas").getContext("2d");
				c.strokeStyle = "#ff0000";
				c.moveTo(x, y);
				c.lineTo(x1, y1);

				c.stroke();
			}

			var getCoordinateshandler = function cnvs_getCoordinates(e) {
				x = e.clientX;
				y = e.clientY;
				draw(x, y);
				document.getElementById("text").innerHTML = "坐标:(" + x + "," + y + ")";
			}

进行添加和移除事件

function lineEvent() {
				var c = document.getElementById("myCanvas");
				c.addEventListener("mousedown", downhandler, false);//添加事件
				c.addEventListener("mouseup", uphandler, false);
				c.removeEventListener("mousemove", getCoordinateshandler, false);//移除事件
			}
以下写法无效:

function lineEvent() {
				var c = document.getElementById("myCanvas");
				c.addEventListener("mousedown", cnvs_down(e), false);//添加事件
				c.addEventListener("mouseup", cnvs_up(e), false);
				c.removeEventListener("mousemove", cnvs_getCoordinates(e), false);//移除事件
			}

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<script type="text/javascript">
			var startX = 0;
			var startY = 0;
			function draw(x, y) {
				var c = document.getElementById("myCanvas").getContext("2d");
				c.fillStyle = "#ff0000";
				c.fillRect(x, y, 2, 2);
			}

			var downhandler = function(e) {
				cnvs_down(e);
			}
			function cnvs_down(e) {
				x = e.clientX;
				y = e.clientY;
				draw(x, y);
				startX = e.clientX;
				startY = e.clientY;
			}

			var uphandler = function(e) {
				cnvs_up(e);
			}
			function cnvs_up(e) {
				x = startX;
				y = startY;
				x1 = e.clientX;
				y1 = e.clientY;
				var c = document.getElementById("myCanvas").getContext("2d");
				c.strokeStyle = "#ff0000";
				c.moveTo(x, y);
				c.lineTo(x1, y1);

				c.stroke();
			}

			var getCoordinateshandler = function cnvs_getCoordinates(e) {
				x = e.clientX;
				y = e.clientY;
				draw(x, y);
				document.getElementById("text").innerHTML = "坐标:(" + x + "," + y + ")";
			}
			function cnvs_clearCoordinates(e) {
				document.getElementById("text").innerHTML = "";
			}

			function lineEvent() {
				var c = document.getElementById("myCanvas");
				c.addEventListener("mousedown", downhandler, false);
				c.addEventListener("mouseup", uphandler, false);
				c.removeEventListener("mousemove", getCoordinateshandler, false);
			}

			function circelEvent() {
				var c = document.getElementById("myCanvas");
				c.addEventListener("mousemove", getCoordinateshandler, false);
				c.removeEventListener("mousedown", downhandler, false);
				c.removeEventListener("mouseup", uphandler, false);
			}

			lineEvent();
		</script>
	</head>
	<body>
		<canvas id="myCanvas" οnmοusedοwn="cnvs_down(event)" οnmοuseup="cnvs_up(event)"  style="float: left;border: 1px solid #c3c3c3" width="480" height="320"></canvas>
		<div id="text"></div>
		<input id="linebtn" type="button" value="直线" οnclick="lineEvent()"/>
		<input id="circelbtn" type="button" value="曲线" οnclick="circelEvent()"/>
	</body>
</html>
参考  http://blog.csdn.net/wl110231/article/details/7597143

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值