Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

HTMLDialogElement

我們的志工尚未將此文章翻譯為 正體中文 (繁體) 版本。加入我們,幫忙翻譯!

This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.

The HTMLDialogElement interface provides methods to manipulate <dialog> elements. It inherits properties and methods from the HTMLElement interface.

Properties

Inherits properties from its parent, HTMLElement.

HTMLDialogElement.open
Is a Boolean reflecting the open HTML attribute, indicating that the dialog is available for interaction.
HTMLDialogElement.returnValue
Is a DOMString that sets or returns the return value for the dialog.

Methods

Inherits methods from its parent, HTMLElement.

Name & Arguments Return Description
close() HTML5 void Closes the dialog. An optional DOMString may be passed as an argument, updating the returnValue of the the dialog.
show() HTML5 void Displays the dialog modelessly, i.e. still allowing interaction with content outside of the dialog. An optional Element or MouseEvent may be passed as an argument, to specify an anchor point to which the dialog is fixed.
showModal() HTML5 void Displays the dialog for exclusive interaction, over the top of any other dialogs that might be present. An optional Element or MouseEvent may be passed as an argument, to specify an anchor point to which the dialog is fixed.

Examples

Example 1

<!-- Anchor point example -->
<dialog id="bronteDialog">
  <p>That was part of a poem by Emily Brontë!</p>
</dialog>

<blockquote>
  <p>"Then art thou glad to seek repose?<br>
  Art glad to leave the sea,<br>
  And <strong id="anchor">anchor</strong> all thy weary woes<br>
  In calm Eternity?"</p>
</blockquote>

<menu>
  <button id="showDialogButton">Show dialog</button>
</menu>

<script>
  (function() {
    var showDialogButton = document.getElementById('showDialogButton');

    // 'Show dialog' button opens dialog, anchored at third line of quote
    showDialogButton.addEventListener('click', function() {
      var bronteDialog = document.getElementById('bronteDialog');
      var anchorPoint = document.getElementById('anchor');
      bronteDialog.show(anchorPoint);
    });

  })();
</script>

Example 2

<!-- Simple pop-up dialog box, containing a form -->
<dialog id="favDialog">
  <form method="dialog">
    <section>
      <p><label for="favAnimal">Favorite animal:</label>
      <select id="favAnimal" name="favAnimal">
        <option></option>
        <option>Brine shrimp</option>
        <option>Red panda</option>
        <option>Spider monkey</option>
      </select></p>
    </section>
    <menu>
      <button id="cancel" type="reset">Cancel</button>
      <button type="submit">Confirm</button>
    </menu>
  </form>
</dialog>

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

<script>
  (function() {
    var updateButton = document.getElementById('updateDetails');
    var cancelButton = document.getElementById('cancel');

    // Update button opens a modal dialog
    updateButton.addEventListener('click', function() {
      document.getElementById('favDialog').showModal();
    });

    // Form cancel button closes the dialog box
    cancelButton.addEventListener('click', function() {
      document.getElementById('favDialog').close();
    });

  })();
</script>

Specifications

Specification Status Comment
WHATWG HTML Living Standard
The definition of '<dialog>' in that specification.
Living Standard  
HTML5.1
The definition of '<dialog>' in that specification.
Working Draft  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 37 No support bug 840640 No support 24 No support
Anchor points No support No support No support No support No support
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support No support No support No support No support No support
Anchor points No support No support No support No support No support

See also

  • The HTML element implementing this interface: <dialog>.

文件標籤與貢獻者

 此頁面的貢獻者: slimsag, fscholz, MHasan, dhodder
 最近更新: slimsag,