<dialog>: The Dialog element - HTML: HyperText Markup Language 编辑

The HTML <dialog> element represents a dialog box or other interactive component, such as a dismissable alert, inspector, or subwindow.

Content categoriesFlow content, sectioning root
Permitted contentFlow content
Tag omissionNone, both the starting and ending tag are mandatory.
Permitted parentsAny element that accepts flow content
Implicit ARIA roledialog
Permitted ARIA rolesalertdialog
DOM interfaceHTMLDialogElement

Attributes

This element includes the global attributes.

The tabindex attribute must not be used on the <dialog> element.

open
Indicates that the dialog is active and can be interacted with. When the open attribute is not set, the dialog shouldn't be shown to the user.

Usage notes

  • <form> elements can close a dialog if they have the attribute method="dialog". When such a form is submitted, the dialog closes with its returnValue property set to the value of the button that was used to submit the form.
  • The ::backdrop CSS pseudo-element can be used to style behind a <dialog> element when the dialog is displayed with HTMLDialogElement.showModal(). For example, to dim unreachable content behind the modal dialog.

Examples

Simple example

<dialog open>
  <p>Greetings, one and all!</p>
</dialog>

Advanced example

This example opens a pop-up dialog box that contains a form, when the "Update details" button is clicked.

HTML

<!-- Simple pop-up dialog box containing a form -->
<dialog id="favDialog">
  <form method="dialog">
    <p><label>Favorite animal:
      <select>
        <option></option>
        <option>Brine shrimp</option>
        <option>Red panda</option>
        <option>Spider monkey</option>
      </select>
    </label></p>
    <menu>
      <button value="cancel">Cancel</button>
      <button id="confirmBtn" value="default">Confirm</button>
    </menu>
  </form>
</dialog>

<menu>
  <button id="updateDetails">Update details</button>
</menu>

<output aria-live="polite"></output>

JavaScript

var updateButton = document.getElementById('updateDetails');
var favDialog = document.getElementById('favDialog');
var outputBox = document.querySelector('output');
var selectEl = document.querySelector('select');
var confirmBtn = document.getElementById('confirmBtn');

// "Update details" button opens the <dialog> modally
updateButton.addEventListener('click', function onOpen() {
  if (typeof favDialog.showModal === "function") {
    favDialog.showModal();
  } else {
    alert("The <dialog> API is not supported by this browser");
  }
});
// "Favorite animal" input sets the value of the submit button
selectEl.addEventListener('change', function onSelect(e) {
  confirmBtn.value = selectEl.value;
});
// "Confirm" button of form triggers "close" on dialog because of [method="dialog"]
favDialog.addEventListener('close', function onClose() {
  outputBox.value = favDialog.returnValue + " button clicked - " + (new Date()).toString();
});

Result

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of '<dialog>' in that specification.
Living Standard
HTML 5.2
The definition of '<dialog>' in that specification.
RecommendationInitial definition

Browser compatibility

BCD tables only load in the browser

Polyfill

Include this polyfill to provide support for browsers without <dialog> element.

dialog-polyfill

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:60 次

字数:7823

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文