0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Javascript 事件處理 - Event Object 事件物件 By 彭彭 21

Posted at
7. 
	a. 事件物件 Event Object

WS000017.JPG

鍵盤keyCode

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8"></meta>
	<title>事件物件 Event Object</title>
	<script type="text/javascript">
	//alert("hello");
	function init(e){
		var btn = document.getElementById("btn");
		var handler = function(e){
			// 滑鼠坐標位置 clientX,clientY
			alert(e.clientX + "," + e.clientY);
		}
		btn.addEventListener("click",handler);
	}
	/*	想象以上的代碼含義
		1. 使用者點擊了按鈕,出發click事件
		2. 瀏覽器主動收集和事件有關的資訊,並製造出event object 事物事件
			var eventObj = 事物事件
		3. 呼叫已經注冊的時間處理器(事件處理函式)
			handler(eventObj);
	*/
	
	// 注冊鍵盤事件處理器 (取得使用者的按鍵code) 鍵盤keyCode
	document.addEventListener("keydown",function(e){
		alert(e.keyCode);
	});

	</script>
</head>
<body onload = "init();">
	<button id = "btn">florence</button>
</body>
</html>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?