Skip to content

事件

一句话解释

事件是 signal 浏览器发送当 某事 happens — 点击, 键按下, page 加载, and 所以 on.

用大白话说

event 是一种notification that something has happened in the browser or in your program。 Clicks, key presses, mouse movements, form submissions, page loads, timer expirations, and network responses all generate events.

JavaScript lets you "listen" for events and run code in response. You register an event listener with 元素.addEventListener('点击', 处理函数). When the user clicks that element, the browser calls your handler function with an event object containing details about what happened.

The event object carries useful information: which key was pressed, the mouse coordinates, the target element, and more. You can also stop default behavior (e.preventDefault()) or stop the event from bubbling up the DOM (e.stopPropagation()).

示例

javascript
const button = document.querySelector("button");

button.addEventListener("click", (event) => {
  console.log("Button clicked!");
  console.log("Clicked element:", event.target);
});

当 button 是点击, 浏览器 fires 点击 事件 and calls 处理函数使用事件对象.

相关术语

相关资源

相关课程

  • DOM

    事件是 bridge 之间 DOM and JavaScript

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表