1. 4.11 Interactive elements
      1. 4.11.1 The details element
      2. 4.11.2 The summary element
      3. 4.11.3 命令
        1. 4.11.3.1 Facets
        2. 4.11.3.2 使用 a 元素来定义命令
        3. 4.11.3.3 使用 button 元素来定义命令
        4. 4.11.3.4 使用 input 元素来定义命令
        5. 4.11.3.5 Using the option element to define a command
        6. 4.11.3.6legend 元素上 使用 accesskey 属性 来定义一个命令
        7. 4.11.3.7 使用 accesskey 属性给其他元素定义命令
      4. 4.11.4 The dialog element

4.11 Interactive elements

4.11.1 The details element

Element/details

Support in all current engines.

Firefox49+Safari6+Chrome12+
Opera15+Edge79+
Edge (Legacy)NoInternet ExplorerNo
Firefox Android49+Safari iOS6+Chrome AndroidYesWebView AndroidYesSamsung InternetYesOpera Android14+

HTMLDetailsElement

Support in all current engines.

Firefox49+Safari6+Chrome10+
Opera15+Edge79+
Edge (Legacy)NoInternet ExplorerNo
Firefox Android49+Safari iOS6+Chrome Android18+WebView Android37+Samsung Internet1.0+Opera Android14+
Categories:
Flow content.
Sectioning root.
Interactive content.
Palpable content.
Contexts in which this element can be used:
Where flow content is expected.
Content model:
One summary element followed by flow content.
text/html 中的标签省略
哪个标签都不能省略。
Content attributes:
Global attributes
open — Whether the details are visible
Accessibility considerations:
For authors.
For implementers.
DOM interface:
[Exposed=Window]
interface HTMLDetailsElement : HTMLElement {
  [HTMLConstructor] constructor();

  [CEReactions] attribute boolean open;
};

The details element represents a disclosure widget from which the user can obtain additional information or controls.

The details element is not appropriate for footnotes. Please see the section on footnotes for details on how to mark up footnotes.

The first summary element child of the element, if any, represents the summary or legend of the details. If there is no child summary element, the user agent should provide its own legend (e.g. "Details").

The rest of the element's contents represents the additional information or controls.

The open content attribute is a boolean attribute. If present, it indicates that both the summary and the additional information is to be shown to the user. If the attribute is absent, only the summary is to be shown.

When the element is created, if the attribute is absent, the additional information should be hidden; if the attribute is present, that information should be shown. Subsequently, if the attribute is removed, then the information should be hidden; if the attribute is added, the information should be shown.

The user agent should allow the user to request that the additional information be shown or hidden. To honor a request for the details to be shown, the user agent must set the open attribute on the element to the empty string. To honor a request for the information to be hidden, the user agent must remove the open attribute from the element.

This ability to request that additional information be shown or hidden may simply be the activation behavior of the appropriate summary element, in the case such an element exists. However, if no such element exists, user agents can still provide this ability through some other user interface affordance.

Whenever the open attribute is added to or removed from a details element, the user agent must queue an element task on the DOM manipulation task source given then details element that runs the following steps, which are known as the details notification task steps, for this details element:

  1. If another task has been queued to run the details notification task steps for this details element, then return.

    When the open attribute is toggled several times in succession, these steps essentially get coalesced so that only one event is fired.

  2. Fire an event named toggle at the details element.

The open IDL attribute must reflect the open content attribute.

The following example shows the details element being used to hide technical details in a progress report.

<section class="progress window">
 <h1>Copying "Really Achieving Your Childhood Dreams"</h1>
 <details>
  <summary>Copying... <progress max="375505392" value="97543282"></progress> 25%</summary>
  <dl>
   <dt>Transfer rate:</dt> <dd>452KB/s</dd>
   <dt>Local filename:</dt> <dd>/home/rpausch/raycd.m4v</dd>
   <dt>Remote filename:</dt> <dd>/var/www/lectures/raycd.m4v</dd>
   <dt>Duration:</dt> <dd>01:16:27</dd>
   <dt>Color profile:</dt> <dd>SD (6-1-6)</dd>
   <dt>Dimensions:</dt> <dd>320×240</dd>
  </dl>
 </details>
</section>

The following shows how a details element can be used to hide some controls by default:

<details>
 <summary><label for=fn>Name & Extension:</label></summary>
 <p><input type=text id=fn name=fn value="Pillar Magazine.pdf">
 <p><label><input type=checkbox name=ext checked> Hide extension</label>
</details>

One could use this in conjunction with other details in a list to allow the user to collapse a set of fields down to a small set of headings, with the ability to open each one.

In these examples, the summary really just summarizes what the controls can change, and not the actual values, which is less than ideal.

Because the open attribute is added and removed automatically as the user interacts with the control, it can be used in CSS to style the element differently based on its state. Here, a style sheet is used to animate the color of the summary when the element is opened or closed:

<style>
 details > summary { transition: color 1s; color: black; }
 details[open] > summary { color: red; }
</style>
<details>
 <summary>Automated Status: Operational</summary>
 <p>Velocity: 12m/s</p>
 <p>Direction: North</p>
</details>

4.11.2 The summary element

Element/summary

Support in all current engines.

Firefox49+Safari6+Chrome12+
Opera15+Edge79+
Edge (Legacy)NoInternet ExplorerNo
Firefox Android49+Safari iOSYesChrome AndroidYesWebView Android4+Samsung InternetYesOpera Android14+
Categories:
None.
Contexts in which this element can be used:
As the first child of a details element.
Content model:
Phrasing content, optionally intermixed with heading content.
text/html 中的标签省略
哪个标签都不能省略。
Content attributes:
Global attributes
Accessibility considerations:
For authors.
For implementers.
DOM interface:
Uses HTMLElement.

The summary element represents a summary, caption, or legend for the rest of the contents of the summary element's parent details element, if any.

A summary element is a summary for its parent details if the following algorithm returns true:

  1. If this summary element has no parent, then return false.

  2. Let parent be this summary element's parent.

  3. If parent is not a details element, then return false.

  4. If parent's first summary element child is not this summary element, then return false.

  5. Return true.

The activation behavior of summary elements is to run the following steps:

  1. If this summary element is not the summary for its parent details, then return.

  2. Let parent be this summary element's parent.

  3. If the open attribute is present on parent, then remove it. Otherwise, set parent's open attribute to the empty string.

    This will then run the details notification task steps.

4.11.3 命令

4.11.3.1 Facets

A command is the abstraction behind menu items, buttons, and links. Once a command is defined, other parts of the interface can refer to the same command, allowing many access points to a single feature to share facets such as the Disabled State.

Commands are defined to have the following facets:

Label
The name of the command as seen by the user.
Access Key
A key combination selected by the user agent that triggers the command. A command might not have an Access Key.
Hidden State
Whether the command is hidden or not (basically, whether it should be shown in menus).
Disabled State
Whether the command is relevant and can be triggered or not.
Action
The actual effect that triggering the command will have. This could be a scripted event handler, a URL to which to navigate, or a form submission.

User agents may expose the commands that match the following criteria:

User agents are encouraged to do this especially for commands that have Access Keys, as a way to advertise those keys to the user.

For example, such commands could be listed in the user agent's menu bar.

4.11.3.2 使用 a 元素来定义命令

href 属性的 a 元素 会定义命令

这个命令的 Label 是该元素的 textContent IDL 属性给出的字符串。

这个命令的 AccessKey 是该元素 被指定的访问键,如果有的话。

如果这个元素有 hidden 属性, 它的 Hidden State 就是 true(隐藏), 否则是 false。

如果这个元素或它的一个祖先是 不活动的, 那么这个命令的 Disabled State 就是 true, 否则是 false。

这个命令的 Action 会在这个元素上 触发一个 click 事件

4.11.3.3 使用 button 元素来定义命令

button 元素总会 定义一个命令

这个命令的 Label, Access Key, Hidden StateAction 的确定方式与 a 元素 相同(参考前一章)。

如果这个元素或它的一个祖先是 不活动的, 或者设置了这个元素的 disabled 状态, 那么这个命令的 Disabled State 就是 true, 否则是 false。

4.11.3.4 使用 input 元素来定义命令

type 属性处于 Submit Button, Reset Button, Image Button, Button, Radio Button, 或 Checkbox 状态之一的 input 元素 定义了命令

这个命令的 Label 按如下过程确定:

这个命令的 AccessKey 就是这个元素 被指定的访问键(如果有的话)。

如果这个元素有 hidden 属性, 它的 Hidden State 就是 true(隐藏), 否则是 false。

如果这个元素或它的一个祖先是 不活动的, 或者设置了这个元素的 disabled 状态, 那么这个命令的 Disabled State 就是 true, 否则是 false。

这个命令的 Action 会在这个元素上 触发一个 click 事件

4.11.3.5 Using the option element to define a command

An option element with an ancestor select element and either no value attribute or a value attribute that is not the empty string defines a command.

The Label of the command is the value of the option element's label attribute, if there is one, or else the option element's descendant text content, with ASCII whitespace stripped and collapsed.

The Access Key of the command is the element's assigned access key, if any.

The Hidden State of the command is true (hidden) if the element has a hidden attribute, and false otherwise.

The Disabled State of the command is true if the element is disabled, or if its nearest ancestor select element is disabled, or if it or one of its ancestors is inert, and false otherwise.

If the option's nearest ancestor select element has a multiple attribute, the Action of the command is to toggle the option element. Otherwise, the Action is to pick the option element.

4.11.3.6legend 元素上 使用 accesskey 属性 来定义一个命令

指定的访问键legend 元素, 如果其祖先 fieldset 元素有一个不是 labellegend 的后代元素(而且不是 legend 元素的后代元素),而且这个后代元素 定义了一个命令,那么这个 legend 元素就 定义了一个命令

命令的 Label 是该元素的 textContent IDL 属性给出的字符串。

命令的 AccessKey 是该元素的 指定的访问键

命令的 Hidden State, Disabled StateActionlegend 元素的父元素的 按照树序 的第一个 定义了命令 的, 而且不是 legend 元素的后代,也不是 labellegend 元素的, 后代元素的相应属性相同。

4.11.3.7 使用 accesskey 属性给其他元素定义命令

指定的访问键 的元素 定义了一个命令

如果前面的章节中定义了一个元素 会定义命令, 那么应该遵循对应章节的定义,并忽略本小节。否则,本小节就适用于那个元素。

命令的 Label 取决于元素本身。 如果该元素是 有标签的控件, 那么 Label 就是给这个控件 打标签label 元素中, 按照 树序 的第一个元素的 textContent 给出的字符串。 (在 DOM 术语中,这个字符串就是 element.labels[0].textContent)。 否则 Label 就是元素自己的 textContent

命令的 AccessKey 就是元素的 被指定的访问键

如果这个元素有 hidden 属性, 它的 Hidden State 就是 true(隐藏), 否则是 false。

如果这个元素或它的一个祖先是 不活动的, 那么这个命令的 Disabled State 就是 true, 否则是 false。

这个命令的 Action 是执行以下步骤:

  1. 为这个元素执行 聚焦步骤
  2. 在这个元素上 触发一个 click 事件

4.11.4 The dialog element

Element/dialog

Support in all current engines.

Firefox98+Safari15.4+Chrome37+
Opera24+Edge79+
Edge (Legacy)NoInternet ExplorerNo
Firefox Android98+Safari iOS15.4+Chrome Android37+WebView Android37+Samsung Internet3.0+Opera Android24+

HTMLDialogElement

Support in all current engines.

Firefox98+Safari15.4+Chrome37+
Opera24+Edge79+
Edge (Legacy)NoInternet ExplorerNo
Firefox Android98+Safari iOS15.4+Chrome Android37+WebView Android37+Samsung Internet3.0+Opera Android24+
Categories:
Flow content.
Sectioning root.
Contexts in which this element can be used:
Where flow content is expected.
Content model:
Flow content.
text/html 中的标签省略
哪个标签都不能省略。
Content attributes:
Global attributes
open — Whether the dialog box is showing
Accessibility considerations:
For authors.
For implementers.
DOM interface:
[Exposed=Window]
interface HTMLDialogElement : HTMLElement {
  [HTMLConstructor] constructor();

  [CEReactions] attribute boolean open;
  attribute DOMString returnValue;
  [CEReactions] undefined show();
  [CEReactions] undefined showModal();
  [CEReactions] undefined close(optional DOMString returnValue);
};

The dialog element represents a part of an application that a user interacts with to perform a task, for example a dialog box, inspector, or window.

The open attribute is a boolean attribute. When specified, it indicates that the dialog element is active and that the user can interact with it.

A dialog element without an open attribute specified should not be shown to the user. This requirement may be implemented indirectly through the style layer. For example, user agents that support the suggested default rendering implement this requirement using the CSS rules described in the rendering section.

Removing the open attribute will usually hide the dialog. However, doing so has a number of strange additional consequences:

For these reasons, it is generally better to never remove the open attribute manually. Instead, use the close() method to close the dialog, or the hidden attribute to hide it.

The tabindex attribute must not be specified on dialog elements.

dialog . show()

HTMLDialogElement/show

Support in all current engines.

Firefox98+Safari15.4+Chrome37+
Opera24+Edge79+
Edge (Legacy)NoInternet ExplorerNo
Firefox Android98+Safari iOS15.4+Chrome Android37+WebView Android37+Samsung Internet3.0+Opera Android24+

Displays the dialog element.

dialog . showModal()

HTMLDialogElement/showModal

Support in all current engines.

Firefox98+Safari15.4+Chrome37+
Opera24+Edge79+
Edge (Legacy)NoInternet ExplorerNo
Firefox Android98+Safari iOS15.4+Chrome Android37+WebView Android37+Samsung Internet3.0+Opera Android24+

Displays the dialog element and makes it the top-most modal dialog.

This method honors the autofocus attribute.

dialog . close( [ result ] )

HTMLDialogElement/close

Support in all current engines.

Firefox98+Safari15.4+Chrome37+
Opera24+Edge79+
Edge (Legacy)NoInternet ExplorerNo
Firefox Android98+Safari iOS15.4+Chrome Android37+WebView Android37+Samsung Internet3.0+Opera Android24+

Closes the dialog element.

The argument, if provided, provides a return value.

dialog . returnValue [ = result ]

HTMLDialogElement/returnValue

Support in all current engines.

Firefox98+Safari15.4+Chrome37+
Opera24+Edge79+
Edge (Legacy)NoInternet ExplorerNo
Firefox Android98+Safari iOS15.4+Chrome Android37+WebView Android37+Samsung Internet3.0+Opera Android24+

Returns the dialog's return value.

Can be set, to update the return value.

When the show() method is invoked, the user agent must run the following steps:

  1. If the element already has an open attribute, then return.

  2. Add an open attribute to the dialog element, whose value is the empty string.

  3. Run the dialog focusing steps for the dialog element.

When the showModal() method is invoked, the user agent must run the following steps:

  1. Let subject be the dialog element on which the method was invoked.

  2. If subject already has an open attribute, then throw an "InvalidStateError" DOMException.

  3. If subject is not connected, then throw an "InvalidStateError" DOMException.

  4. Add an open attribute to subject, whose value is the empty string.

  5. Set the is modal flag of subject to true.

  6. Let subject's node document be blocked by the modal dialog subject.

    This will cause the focused area of the document to become inert (unless that currently focused area is a shadow-including descendant of subject). In such cases, the focus fixup rule will kick in and reset the focused area of the document to the viewport for now. In a couple steps we will attempt to find a better candidate to focus.

  7. If subject's node document's top layer does not already contain subject, then add subject to subject's node document's top layer.

  8. Run the dialog focusing steps for subject.

The dialog focusing steps for a dialog element subject are as follows:

  1. If subject is inert, return.

  2. Let control be the first descendant element of subject, in tree order, that is not inert and has the autofocus attribute specified.

    If there isn't one, then let control be the first non-inert descendant element of subject, in tree order.

    If there isn't one of those either, then let control be subject.

  3. Run the focusing steps for control.

    If control is not focusable, this will do nothing. For modal dialogs, this means that any earlier modifications to the focused area of the document will apply.

  4. Let topDocument be the active document of control's node document's browsing context's top-level browsing context.

  5. If control's node document's origin is not the same as the origin of topDocument, then return.

  6. Empty topDocument's autofocus candidates.

  7. Set topDocument's autofocus processed flag to true.

If at any time a dialog element is removed from a Document, then if that dialog is in that Document's top layer, it must be removed from it.

When the close() method is invoked, the user agent must close the dialog that the method was invoked on. If the method was invoked with an argument, that argument must be used as the return value; otherwise, there is no return value.

When a dialog element subject is to be closed, optionally with a return value result, the user agent must run the following steps:

  1. If subject does not have an open attribute, then return.

  2. Remove subject's open attribute.

  3. Set the is modal flag of subject to false.

  4. If the argument result was provided, then set the returnValue attribute to the value of result.

  5. If subject is in its Document's top layer, then remove it.

  6. Queue an element task on the user interaction task source given the subject element to fire an event named close at subject.

The returnValue IDL attribute, on getting, must return the last value to which it was set. On setting, it must be set to the new value. When the element is created, it must be set to the empty string.


Canceling dialogs: When Document is blocked by a modal dialog dialog, user agents may provide a user interface that, upon activation, queues an element task on the user interaction task source given the dialog element to run these steps:

  1. Let close be the result of firing an event named cancel at dialog, with the cancelable attribute initialized to true.

  2. If close is true and dialog has an open attribute, then close the dialog with no return value.

An example of such a UI mechanism would be the user pressing the "Escape" key.


Each dialog element has an is modal flag. When a dialog element is created, this flag must be set to false.


HTMLDialogElement/open

Support in all current engines.

Firefox98+Safari15.4+Chrome37+
Opera24+Edge79+
Edge (Legacy)NoInternet ExplorerNo
Firefox Android98+Safari iOS15.4+Chrome Android37+WebView Android37+Samsung Internet3.0+Opera Android24+

The open IDL attribute must reflect the open content attribute.

This dialog box has some small print. The strong element is used to draw the user's attention to the more important part.

<dialog>
 <h1>Add to Wallet</h1>
 <p><strong><label for=amt>How many gold coins do you want to add to your wallet?</label></strong></p>
 <p><input id=amt name=amt type=number min=0 step=0.01 value=100></p>
 <p><small>You add coins at your own risk.</small></p>
 <p><label><input name=round type=checkbox> Only add perfectly round coins </label></p>
 <p><input type=button onclick="submit()" value="Add Coins"></p>
</dialog>