Event: thuộc tính type
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Note: This feature is available in Web Workers.
Thuộc tính chỉ đọc type của giao diện Event trả về một chuỗi chứa loại của sự kiện. Nó được đặt khi sự kiện được khởi tạo và là tên thường dùng để chỉ sự kiện cụ thể đó, chẳng hạn
click, load, hoặc error.
Giá trị
Một chuỗi chứa loại của Event.
Ví dụ
Ví dụ này ghi lại loại sự kiện mỗi khi bạn nhấn một phím trên bàn phím hoặc nhấp nút chuột.
HTML
html
<p>Nhấn bất kỳ phím nào hoặc nhấp chuột để lấy loại sự kiện.</p>
<p id="log"></p>
JavaScript
js
function getEventType(event) {
const log = document.getElementById("log");
log.innerText = `${event.type}\n${log.innerText}`;
}
// Sự kiện bàn phím
document.addEventListener("keydown", getEventType); // đầu tiên
document.addEventListener("keypress", getEventType); // thứ hai
document.addEventListener("keyup", getEventType); // thứ ba
// Sự kiện chuột
document.addEventListener("mousedown", getEventType); // đầu tiên
document.addEventListener("mouseup", getEventType); // thứ hai
document.addEventListener("click", getEventType); // thứ ba
Kết quả
Thông số kỹ thuật
| Specification |
|---|
| DOM> # ref-for-dom-event-type④> |