# 5.2 事件物件(Event Object)

## 認識事件物件

任何的事件發生時，都會隨附一個事件物件(Event Object)，它會提供的額外資訊，依照事件類型的不同，會帶有不同的資料。

## 基本事件寫法

```javascript
var my_el = document.getElementById("abc");
my_el.addEventListener("click", function(){
  
});
```

如果在某些情境當中，需要使用到事件物件，那就可以帶入函式的第一個參數，例：

```javascript
var my_el = document.getElementById("abc");
my_el.addEventListener("click", function(e){ // e 就是 event object
  console.log(e); // 將 e 印在 console 中看
});
```

## 範例：取得鍵盤按下的 ASCII Code 碼

透過 **`e.which`** 來取得 keyup 事件發生時的 ASCII Code 碼：

{% embed url="<https://codepen.io/carlos411/pen/GRZrOwv>" %}

參考：[w3schools Event Object](https://www.w3schools.com/JSREF/obj_event.asp)。(看看網址裡面的，找一下 `e.target` 是什麼？)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.webmix.cc/javascript-web/5.-shi-jian/5.2-shi-jian-wu-jian-event-object.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
