WHAT - Typescript 中 Window | Document | HTMLElement | EventTarget

In the context of web development, Window, Document, HTMLElement, and EventTarget are important interfaces provided by the browser's DOM (Document Object Model) and event handling systems. Each of these interfaces serves a distinct purpose and has its own role in interacting with web pages.

Here's a detailed explanation of each:

1. Window

Window is the global object in a web browser that represents the browser window or tab. It provides access to various properties and methods that are fundamental to interacting with the browser environment.

Key Features:
  • Global Scope : The Window object is the global object in the browser, which means that all global variables and functions are properties of the Window object.
  • Properties : Includes properties like document (to access the Document object), location (to get or set the current URL), and localStorage (for local storage).
  • Methods : Includes methods like alert(), confirm(), setTimeout(), open(), and close().
Example:
javascript 复制代码
console.log(window.innerWidth); // Get the width of the browser window
window.alert("Hello, World!"); // Show an alert dialog

2. Document

Document represents the web page loaded in the browser window. It provides access to the contents of the web page and is the main entry point for manipulating the DOM.

Key Features:
  • DOM Access : Provides methods and properties to access and manipulate elements within the web page. For example, getElementById(), querySelector(), and createElement().
  • Properties : Includes properties like body (the <body> element), title (the title of the document), and cookie (to get or set cookies).
  • Events : The Document object can also handle events such as DOMContentLoaded.
Example:
javascript 复制代码
console.log(document.title); // Get the title of the document
document.getElementById("myElement").innerText = "Hello"; // Modify an element's text

3. HTMLElement

HTMLElement is an interface representing an element in an HTML document. It extends the Element interface with properties and methods specific to HTML elements.

Key Features:
  • Properties : Includes properties like innerHTML, className, id, style, and attributes.
  • Methods : Provides methods such as getAttribute(), setAttribute(), appendChild(), and removeChild().
  • Events : HTML elements can have event handlers like onclick, onmouseover, etc.
Example:
javascript 复制代码
const element = document.getElementById("myElement");
element.innerHTML = "New Content"; // Change the content of the element
element.style.color = "blue"; // Change the text color of the element

4. EventTarget

EventTarget is an interface implemented by objects that can receive and handle events. It is the base interface for all objects that can participate in event handling, such as Window, Document, and HTMLElement.

Key Features:
  • Event Handling : Provides methods for adding and removing event listeners, such as addEventListener() and removeEventListener().
  • Event Propagation: Allows objects to be targets of events and participate in the event propagation process (capturing, target, and bubbling phases).
Example:
javascript 复制代码
const button = document.getElementById("myButton");
button.addEventListener("click", function(event) {
  console.log("Button clicked!");
});

Relationships and Hierarchies

  • Window → Represents the browser window and contains the Document object.
  • Document → Represents the HTML document and contains HTMLElement objects.
  • HTMLElement → Represents HTML elements and extends the Element interface.
  • EventTarget → Base interface for objects that handle events. Both Document and HTMLElement implement EventTarget.

Summary

  • Window: Represents the browser window or tab, providing access to the global scope and browser-related functions.
  • Document: Represents the web page loaded in the browser, allowing manipulation of the DOM.
  • HTMLElement: Represents individual HTML elements within the document, providing properties and methods for element-specific manipulations.
  • EventTarget : The base interface for objects that can handle events, including Window, Document, and HTMLElement.

Understanding these interfaces helps in effectively working with the DOM and handling events in web development.

相关推荐
小成202303202654 分钟前
C++~02运算符重载
开发语言·c++
emfuture13 分钟前
龙芯2K300 PLC 在拉丝机收线系统改造中的应用:硬件选型、张力控制与国产化替代实践
java·开发语言·智能制造·plc·国产化替代
fruge15 分钟前
SSH、RDP、VNC不必各开一个客户端:Next Terminal用Web入口统一管理远程资产和会话记录
运维·前端·ssh
geovindu27 分钟前
rust: Mediator Pattern
开发语言·后端·设计模式·rust·中介者模式·行为模式
honkun632 分钟前
vue 下拉树组件 vxe-table-select 实现分页多选
前端·javascript·vue.js
传奇开心果编程37 分钟前
【Rust入门练中学】第8课:错误处理
开发语言·rust·错误处理·编程语言理论·通用编程语言
大熊程序猿42 分钟前
用 ASP.NET Core + SignalR 做一个带实时对战房间的打字练习平台
javascript
OpsEye1 小时前
大量无效 Token 消耗,是很多企业被忽略的隐形成本
javascript·ai编程
郑同学的笔记1 小时前
【c++随笔30】C++ 注册回调函数
开发语言·c++
用户621007587021 小时前
Web BIM 开发实践:如何将构件选择、属性查询接入业务系统?
前端