大网数据广告活动
Web 标准与前端开发

HTMLDialogElement: show() method

· 52CE 我爱测

The **`show()`** method of the HTMLDialogElement interface displays the dialog as a non-modal dialog.

HTML DOM

The show() method of the HTMLDialogElement interface displays the dialog as a non-modal dialog.

A non-modal dialog is one where users can interact with content outside/behind the open dialog.

Syntax

show()

Parameters

None.

Return value

None (undefined).

Exceptions

  • InvalidStateError DOMException
    • : Thrown if the dialog is already open and modal (i.e., if the dialog has already been opened with HTMLDialogElement.showModal()).

Examples

Basic usage

The following example shows a simple button that, when clicked, opens a dialog using the show() method.

When the dialog is open, you can still interact with the rest of the page, including clicking the Click me button that triggers an alert.

You can click the Close dialog button to close the dialog (via the HTMLDialogElement.close() method).

HTML

<dialog id="dialog">
  <button type="button" id="close">Close dialog</button>
</dialog>

<p><button id="open">Open dialog</button></p>
<p><button id="alert">Trigger alert</button></p>

JavaScript

const dialog = document.getElementById("dialog");
const openButton = document.getElementById("open");
const closeButton = document.getElementById("close");
const alertButton = document.getElementById("alert");

// Open button opens a modeless dialog
openButton.addEventListener("click", () => {
  dialog.show();
});

// Alert button triggers an alert
alertButton.addEventListener("click", () => {
  alert("you clicked me!");
});

// Close button closes the dialog box
closeButton.addEventListener("click", () => {
  dialog.close();
});

Result

Basic usage

Specifications

Browser compatibility

See also

  • HTML dialog element
  • HTMLDialogElement.showModal()

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。