aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/html/element/menu/index.html
blob: 97c4b0c17318f755a024ae8f0c8ffea1ce56e03d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
---
title: <menu>
slug: Web/HTML/Element/menu
tags:
  - HTML
  - HTML 交互元素
  - UX
  - Web
  - 元素
  - 参考
  - 菜单
translation_of: Web/HTML/Element/menu
---
<div>{{HTMLRef}}{{SeeCompatTable}}</div>

<p><strong>HTML <code>&lt;menu&gt;</code> 元素</strong>呈现了一组用户可执行或激活的命令。这既包含了可能出现在屏幕顶端的列表菜单,也包含了那些隐藏在按钮之下、当点击按钮后显示出来的文本菜单。</p>

<table class="properties">
 <tbody>
  <tr>
   <th scope="row"><a href="/en-US/docs/Web/HTML/Content_categories">Content categories</a></th>
   <td><a href="/en-US/docs/Web/HTML/Content_categories#Flow_content">Flow content</a>. Additionally, if in the <em>list menu</em> state, palpable content. (<em>list menu</em> is the default state, unless the parent element is a {{HTMLElement("menu")}} in the <em>context menu</em> state.)</td>
  </tr>
  <tr>
   <th scope="row">Permitted content</th>
   <td>If the element is in the <em>list menu</em> state: <a href="/en-US/docs/Web/HTML/Content_categories#Flow_content">flow content</a>, or alternatively, zero or more occurrences of {{HTMLElement("li")}}, {{HTMLElement("script")}}, and {{HTMLElement("template")}}.<br>
    If the element is in the <em>context menu</em> state: zero or more occurrences, in any order, of {{HTMLElement("menu")}} (<em>context menu</em> state only), {{HTMLElement("menuitem")}}, {{HTMLElement("hr")}}, {{HTMLElement("script")}}, and {{HTMLElement("template")}}.</td>
  </tr>
  <tr>
   <th scope="row">Tag omission</th>
   <td>{{No_Tag_Omission}}</td>
  </tr>
  <tr>
   <th scope="row">Permitted parents</th>
   <td>Any element that accepts <a href="/en-US/docs/Web/HTML/Content_categories#Flow_content">flow content</a>.</td>
  </tr>
  <tr>
   <th scope="row">Permitted ARIA roles</th>
   <td>None</td>
  </tr>
  <tr>
   <th scope="row">DOM interface</th>
   <td>{{DOMxRef("HTMLMenuElement")}}</td>
  </tr>
 </tbody>
</table>

<h2 id="属性">属性</h2>

<p>该元素支持<a href="/en-US/HTML/Global_attributes">全局属性</a>,以及以下属性。</p>

<dl>
 <dt>{{HTMLAttrDef("label")}} {{Deprecated_inline}}</dt>
 <dd>The name of the menu as shown to the user. Used within nested menus, to provide a label through which the submenu can be accessed. Must only be specified when the parent element is a {{HTMLElement("menu")}} in the <em>context menu</em> state.</dd>
 <dt>{{HTMLAttrDef("type")}}</dt>
 <dd>This attribute indicates the kind of menu being declared, and can be one of two values.
 <ul>
  <li><code>context</code> {{Deprecated_inline}} : Indicates the <em>popup menu</em> state, which represents a group of commands activated through another element. This might be as a button menu referenced by a {{HTMLAttrxRef("menu", "button")}} attribute of a {{HTMLElement("button")}} element, or as context menu for an element with a <a href="/en-US/docs/HTML/Global_attributes#attr-contextmenu"><code>contextmenu</code></a> attribute. This value is the default if the attribute is missing and the parent element is also a <code>&lt;menu&gt;</code> element.</li>
  <li><code>toolbar</code>: Indicates the <em>toolbar</em> state, which represents a toolbar consisting of a series of commands for user interaction. This might be in the form of an unordered list of {{HTMLElement("li")}} elements, or, if the element has no <code>&lt;li&gt;</code> element children, flow content describing available commands. This value is the default if the attribute is missing.</li>
 </ul>
 </dd>
</dl>

<h2 id="使用备注">使用备注</h2>

<p>{{HTMLElement("menu")}}{{HTMLElement("ul")}} 元素都呈现了无序列表元素。最主要的区别是,{{HTMLElement("ul")}} 主要是为了展示选项,而 {{HTMLElement("menu")}} 则是为了交互。</p>

<p>An HTML menu can be used to create context menus (typically activated by right-clicking another element) or toolbars.</p>

<p><strong>{{anch("Context menu", "Context menus</strong>")}} consist of a <code>&lt;menu&gt;</code> element which contains {{HTMLElement("menuitem")}} elements for each selectable option in the menu, <code>&lt;menu&gt;</code> elements for submenus within the menu, and {{HTMLElement("hr")}} elements for separator lines to break up the menu's content into sections. Context menus are then attached to the element they're activated from using either the associated element's {{HTMLAttrxRef("contextmenu")}} attribute or, for {{anch("Button menu", "button-activated menus")}} attached to {{HTMLElement("button")}} elements, the {{HTMLAttrxRef("menu", "button")}} attribute.</p>

<p><strong>{{anch("Toolbar", "Toolbar menus</strong>")}} consist of a <code>&lt;menu&gt;</code> element whose content is described in one of two ways: either as an unordered list of items represented by {{HTMLElement("li")}} elements (each representing a command or option the user can utilize), or (if there are no <code>&lt;li&gt;</code> elements), <a href="/en-US/docs/Web/HTML/Content_categories#Flow_content">flow content</a> describing the available commands and options.</p>

<p>这个元素在 HTML4 中被弃用,但在 HTML 5.1 和 HTML living standard 中被重新推荐使用。 这份文档描述的是当前 Firefox 上的实现。根据 HTML 5.1 的规定,{{HTMLElement("list")}} 元素的类型可能会更改为{{HTMLElement("toolbar")}}</p>

<h2 id="例子">例子</h2>

<h3 id="右键菜单">右键菜单</h3>

<p>{{Deprecated_header}}</p>

<h4 id="HTML">HTML</h4>

<pre class="brush: html;">&lt;!-- A &lt;div&gt; element with a context menu --&gt;
&lt;div contextmenu="popup-menu"&gt;
  Right-click to see the adjusted context menu
&lt;/div&gt;

&lt;menu type="context" id="popup-menu"&gt;
  &lt;menuitem&gt;Action&lt;/menuitem&gt;
  &lt;menuitem&gt;Another action&lt;/menuitem&gt;
  &lt;hr/&gt;
  &lt;menuitem&gt;Separated action&lt;/menuitem&gt;
&lt;/menu&gt;
</pre>

<h4 id="CSS">CSS</h4>

<pre class="brush: css;">div {
  width: 300px;
  height: 80px;
  background-color: lightgreen;
}
</pre>

<h4 id="结果">结果</h4>

<p>{{EmbedLiveSample("Context_menu", "100%", 80)}}</p>

<h3 id="菜单按钮">菜单按钮</h3>

<div class="blockIndicator warning">
<p>Menu buttons haven't been implemented in any known browsers yet. The {{HTMLAttrxRef("type", "menu")}} attribute on the <code>&lt;menu&gt;</code> element is now obsolete.</p>
</div>

<div class="blockIndicator warning">
<p>{{HTMLElement("menuitem")}} element is obsolete.</p>
</div>

<h4 id="HTML_2">HTML</h4>

<pre class="brush: html;">&lt;!-- A button, which displays a menu when clicked. --&gt;
&lt;button type="menu" menu="popup-menu"&gt;
  Dropdown
&lt;/button&gt;

&lt;menu type="context" id="popup-menu"&gt;
  &lt;menuitem&gt;Action&lt;/menuitem&gt;
  &lt;menuitem&gt;Another action&lt;/menuitem&gt;
  &lt;hr/&gt;
  &lt;menuitem&gt;Separated action&lt;/menuitem&gt;
&lt;/menu&gt;
</pre>

<h4 id="Result">Result</h4>

<p>{{EmbedLiveSample("Menu_button", "100%", 50)}}</p>

<h3 id="Toolbar" name="Toolbar">Toolbar</h3>

<div class="blockIndicator warning">
<p>Toolbar menus haven't been implemented in any known browsers yet.</p>
</div>

<h4 id="HTML_3">HTML</h4>

<pre class="brush: html;">&lt;!-- A context menu for a simple editor,
   - containing two menu buttons. --&gt;
&lt;menu type="toolbar"&gt;
  &lt;li&gt;
    &lt;button type="menu" menu="file-menu"&gt;File&lt;/button&gt;
    &lt;menu type="context" id="file-menu"&gt;
      &lt;menuitem label="New..." onclick="newFile()"&gt;
      &lt;menuitem label="Save..." onclick="saveFile()"&gt;
    &lt;/menu&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;button type="menu" menu="edit-menu"&gt;Edit&lt;/button&gt;
    &lt;menu type="context" id="edit-menu"&gt;
      &lt;menuitem label="Cut..." onclick="cutEdit()"&gt;
      &lt;menuitem label="Copy..." onclick="copyEdit()"&gt;
      &lt;menuitem label="Paste..." onclick="pasteEdit()"&gt;
    &lt;/menu&gt;
  &lt;/li&gt;
&lt;/menu&gt;
</pre>

<h4 id="结果_2">结果</h4>

<p>{{EmbedLiveSample("Toolbar", "100%", 100)}}</p>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">备注</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName("HTML WHATWG", "grouping-content.html#the-menu-element", "&lt;menu&gt;")}}</td>
   <td>{{Spec2("HTML WHATWG")}}</td>
   <td>No change from latest snapshot, {{SpecName("HTML5.3")}}</td>
  </tr>
  <tr>
   <td>{{SpecName("HTML5.3", "grouping-content.html#the-menu-element", "&lt;menu&gt;")}}</td>
   <td>{{Spec2("HTML5.3")}}</td>
   <td>No changes</td>
  </tr>
  <tr>
   <td>{{SpecName("HTML5.2", "grouping-content.html#the-menu-element", "&lt;menu&gt;")}}</td>
   <td>{{Spec2("HTML5.2")}}</td>
   <td>Snapshot of the {{SpecName("HTML WHATWG")}}.<br>
    Removed the <code>context</code> type.</td>
  </tr>
  <tr>
   <td>{{SpecName("HTML5.1", "interactive-elements.html#the-menu-element", "&lt;menu&gt;")}}</td>
   <td>{{Spec2("HTML5.1")}}</td>
   <td>Snapshot of the {{SpecName("HTML WHATWG")}}.<br>
    Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("html.elements.menu")}}</p>

<h2 id="参见">参见</h2>

<ul>
 <li>Other list-related HTML Elements: {{HTMLElement("ol")}}, {{HTMLElement("ul")}}, {{HTMLElement("li")}}, {{HTMLElement("hr")}}, and the obsolete {{HTMLElement("dir")}}.</li>
 <li>The <a href="/en-US/docs/Web/HTML/Global_attributes#attr-contextmenu"><code>contextmenu</code></a> <a href="/en-US/docs/Web/HTML/Global_attributes">global attribute</a> can be used on an element to refer to the <code>id</code> of a <code>menu</code> with {{HTMLAttrxRef("type", "menu", 'type="context"')}}.</li>
</ul>

<div>{{HTMLRef}}</div>