The HTML DOM API

HTML DOM API HTML DOM API は、HTML の各要素の機能を定義するインターフェイスと、それらが依存する補助的な型やインターフェイスから構成されています。

HTML DOM API に含まれる機能領域には、以下のようなものがあります。

HTML DOM の概念と利用方法

この記事では、HTML DOM の中で HTML 要素に関係する部分に焦点を当てます。ドラッグアンドドロップウェブソケットウェブストレージなどの他の分野についての議論は、それらの API のドキュメントを参照してください。

HTML 文書の構造

Document Object Model (DOM) は、文書の構造を記述する構造で、それぞれの文書は Document インターフェイスのインスタンスで表現されます。文書はノードの階層ツリーで構成され、ノードは文書内の単一のオブジェクト (要素やテキストノードなど) を表す基本レコードです。

ノードは厳密に組織化することができ、他のノードをグループ化する手段を提供したり、階層構造を構築するためのポイントを提供したりします。各ノードは、そのノードに関する情報を取得するためのプロパティと、DOM 内でノードを作成、削除、整理するためのメソッドを提供する Node インタフェースに基づいています。

Nodes don't have any concept of including the content that is actually displayed in the document. They're empty vessels. The fundamental notion of a node that can represent visual content is introduced by the Element interface. An Element object instance represents a single element in a document created using either HTML or an XML vocabulary such as SVG.

For example, consider a document with two elements, one of which has two more elements nested inside it:

Structure of a document with elements inside a document in a window

While the Document interface is defined as part of the DOM specification, the HTML specification significantly enhances it to add information specific to using the DOM in the context of a web browser, as well as to using it to represent HTML documents specifically.

Among the things added to Document by the HTML standard are:

HTML 要素のインターフェイス

The Element interface has been further adapted to represent HTML elements specifically by introducing the HTMLElement interface, which all more specific HTML element classes inherit from. This expands the Element class to add HTML-specific general features to the element nodes. Properties added by HTMLElement include for example hidden and innerText. HTMLElement also adds all the global event handlers.

An HTML document is a DOM tree in which each of the nodes is an HTML element, represented by the HTMLElement interface. The HTMLElement class, in turn, implements Node, so every element is also a node (but not the other way around). This way, the structural features implemented by the Node interface are also available to HTML elements, allowing them to be nested within each other, created and deleted, moved around, and so forth.

The HTMLElement interface is generic, however, providing only the functionality common to all HTML elements such as the element's ID, its coordinates, the HTML making up the element, information about scroll position, and so forth.

In order to expand upon the functionality of the core HTMLElement interface to provide the features needed by a specific element, the HTMLElement class is subclassed to add the needed properties and methods. For example, the <canvas> element is represented by an object of type HTMLCanvasElement. HTMLCanvasElement augments the HTMLElement type by adding properties such as height (en-US) and methods like getContext() (en-US) to provide canvas-specific features.

The overall inheritance for HTML element classes looks like this:

Hierarchy of interfaces for HTML elements

As such, an element inherits the properties and methods of all of its ancestors. For example, consider a <a> element, which is represented in the DOM by an object of type HTMLAnchorElement. The element, then, includes the anchor-specific properties and methods described in that class's documentation, but also those defined by HTMLElement and Element, as well as from Node and, finally, EventTarget.

Each level defines a key aspect of the utility of the element. From Node, the element inherits concepts surrounding the ability for the element to be contained by another element, and to contain other elements itself. Of special importance is what is gained by inheriting from EventTarget: the ability to receive and handle events such as mouse clicks, play and pause events, and so forth.

There are elements that share commonalities and thus have an additional intermediary type. For example, the <audio> and <video> elements both present audiovisual media. The corresponding types, HTMLAudioElement and HTMLVideoElement, are both based upon the common type HTMLMediaElement, which in turn is based upon HTMLElement and so forth. HTMLMediaElement defines the methods and properties held in common between audio and video elements.

These element-specific interfaces make up the majority of the HTML DOM API, and are the focus of this article. To learn more about the actual structure of the DOM, see Introduction to the DOM.

HTML DOM の対象読者

The features exposed by the HTML DOM are among the most commonly-used APIs in the web developer's arsenal. All but the most simple web applications will use some features of the HTML DOM.

HTML DOM API のインターフェイス

The majority of the interfaces that comprise the HTML DOM API map almost one-to-one to individual HTML elements, or to a small group of elements with similar functionality. In addition, the HTML DOM API includes a few interfaces and types to support the HTML element interfaces.

HTML 要素のインターフェイス

These interfaces represent specific HTML elements (or sets of related elements which have the same properties and methods associated with them).

非推奨の HTML 要素のインターフェイス

廃止された HTML 要素のインターフェイス

ウェブアプリとブラウザーの統合インターフェイス

These interfaces offer access to the browser window and document that contain the HTML, as well as to the browser's state, available plugins (if any), and various configuration options.

非推奨のウェブアプリとブラウザーの統合インターフェイス

廃止されたウェブアプリとブラウザーの統合インターフェイス

  • ApplicationCache これは廃止された API であり、動作は保証されていません。
  • Plugin これは廃止された API であり、動作は保証されていません。
  • PluginArray これは廃止された API であり、動作は保証されていません。

フォーム対応インターフェイス

These interfaces provide structure and functionality required by the elements used to create and manage forms, including the <form> and <input> elements.

キャンバスと画像のインターフェイス

These interfaces represent objects used by the Canvas API as well as the <img> element and <picture> elements.

メディアインターフェイス

The media interfaces provide HTML access to the contents of the media elements: <audio> and <video>.

ドラッグ&ドロップインターフェイス

These interfaces are used by the HTML_Drag_and_Drop_API to represent individual draggable (or dragged) items, groups of dragged or draggable items, and to handle the drag and drop process.

ページ履歴インターフェイス

The History API interfaces let you access information about the browser's history, as well as to shift the browser's current tab forward and backward through that history.

ウェブコンポーネントインターフェイス

These interfaces are used by the Web Components API to create and manage the available custom elements.

その他および補助インターフェイス

These supporting object types are used in a variety of ways in the HTML DOM API. In addition, PromiseRejectionEvent represents the event delivered when a JavaScript Promise is rejected.

他の API に所属するインターフェイス

Several interfaces are technically defined in the HTML specification while actually being part of other APIs.

ウェブストレージインターフェイス

The Web_Storage_API provides the ability for web sites to store data either temporarily or permanently on the user's device for later re-use.

ウェブワーカーインターフェイス

These interfaces are used by the Web_Workers_API both to establish the ability for workers to interact with an app and its content, but also to support messaging between windows or apps.

WebSocket インターフェイス

These interfaces, defined by the HTML specification, are used by the WebSockets_API.

Server-sent event インターフェイス

The EventSource interface represents the source which sent or is sending server-sent events.

In this example, an <input> element's input event is monitored in order to update the state of a form's "submit" button based on whether or not a given field currently has a value.

JavaScript

const nameField = document.getElementById("userName");
const sendButton = document.getElementById("sendButton")

sendButton.disabled = true;
// [note: this is disabled since it causes this article to always load with this example focused and scrolled into view]
//nameField.focus();

nameField.addEventListener("input", event => {
  const elem = event.target;
  const valid = elem.value.length != 0;

  if (valid && sendButton.disabled) {
    sendButton.disabled = false;
  } else if (!valid && !sendButton.disabled) {
    sendButton.disabled = true;
  }
});

This code uses the Document interface's getElementById() method to get the DOM object representing the <input> elements whose IDs are userName and sendButton. With these, we can access the properties and methods that provide information about and grant control over these elements.

The HTMLInputElement object for the "Send" button's disabled property is set to true, which disables the "Send" button so it can't be clicked. In addition, the user name input field is made the active focus by calling the focus() method it inherits from HTMLElement.

Then addEventListener() is called to add a handler for the input event to the user name input. This code looks at the length of the current value of the input; if it's zero, then the "Send" button is disabled if it's not already disabled. Otherwise, the code ensures that the button is enabled.

With this in place, the "Send" button is always enabled whenever the user name input field has a value, and disabled when it's empty.

HTML

The HTML for the form looks like this:

<p>Please provide the information below. Items marked with "*" are required.</p>
<form action="" method="get">
  <p>
    <label for="userName" required>Your name:</label>
    <input type="text" id="userName"> (*)
  </p>
  <p>
    <label for="email">Email:</label>
    <input type="email" id="userEmail">
  </p>
  <input type="submit" value="Send" id="sendButton">
</form>

結果

仕様書

仕様書 状態 備考
HTML Living Standard 現行の標準 WHATWG HTML Specification
HTML5 勧告 No change from Document Object Model (DOM) Level 2 HTML Specification
Document Object Model (DOM) Level 2 HTML Specification 廃止 No change from Document Object Model (DOM) Level 1 Specification.
Document Object Model (DOM) Level 1 Specification 廃止 初回定義

ブラウザーの互換性

BCD tables only load in the browser

関連情報

リファレンス
ガイド
  • 文書の操作: DOM を操作するための初心者向けガイドです。