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

HTMLDialogElement: close() method

· 52CE 我爱测

The **`close()`** method of the HTMLDialogElement interface closes the dialog. An optional string may be passed as an argument, updating the HTMLDialogElement.returnValue of the dialog.

HTML DOM

The close() method of the HTMLDialogElement interface closes the dialog. An optional string may be passed as an argument, updating the HTMLDialogElement.returnValue of the dialog.

The HTMLDialogElement.close_event event is fired after the dialog has closed. Unlike when calling HTMLDialogElement.requestClose(), the close operation cannot be canceled.

Syntax

close()
close(returnValue)

Parameters

  • returnValue
    • : A string that replaces the existing value of HTMLDialogElement.returnValue.

Return value

None (undefined).

Examples

Closing a dialog

The following example shows a button that, when clicked, opens a dialog via the HTMLDialogElement.showModal() method. From there you can click the either Close button to close the dialog (via the close() method).

The Close button closes the dialog without a HTMLDialogElement.returnValue, while the Close w/ return value button closes the dialog with a HTMLDialogElement.returnValue.

HTML

<dialog id="dialog">
  <button type="button" id="close">Close</button>
  <button type="button" id="close-w-value">Close w/ return value</button>
</dialog>

<button id="open">Open dialog</button>
<pre id="log"></pre>
#log {
  height: 170px;
  overflow: scroll;
  padding: 0.5rem;
  border: 1px solid black;
}

JavaScript

const logElement = document.getElementById("log");
function log(text) {
  logElement.innerText = `${logElement.innerText}${text}\n`;
  logElement.scrollTop = logElement.scrollHeight;
}
const dialog = document.getElementById("dialog");
const openButton = document.getElementById("open");
const closeButton = document.getElementById("close");
const closeWithValueButton = document.getElementById("close-w-value");

// Update button opens a modal dialog
openButton.addEventListener("click", () => {
  // Reset the return value
  dialog.returnValue = "";
  // Show the dialog
  dialog.showModal();
});

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

// Close button closes the dialog box with a return value
closeWithValueButton.addEventListener("click", () => {
  dialog.close(`Closed at ${new Date().toLocaleTimeString()}`);
});

// Form close button closes the dialog box
dialog.addEventListener("close", () => {
  log(`Dialog closed. Return value: "${dialog.returnValue}"`);
});

[!NOTE]

You know you can also automatically close a <dialog> by submitting a form element with a method="dialog" attribute.

Result

Closing a dialog

Specifications

Browser compatibility

See also

  • HTML dialog element
  • The HTMLDialogElement.close_event event
  • HTMLDialogElement.requestClose()

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

资料来源与适用范围

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

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

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