<menu>: The Menu element

<menu> HTML 元素在 HTML 规范中被描述为 <ul> 的语义替代,但浏览器将其视为(并通过可访问性树公开)与 <ul> 没有什么不同。它表示一个无序列表的项目(由 <li> 个元素表示)。

¥The <menu> HTML element is described in the HTML specification as a semantic alternative to <ul>, but treated by browsers (and exposed through the accessibility tree) as no different than <ul>. It represents an unordered list of items (which are represented by <li> elements).

Try it

属性

¥Attributes

该元素仅包含 全局属性

¥This element only includes the global attributes.

使用说明

¥Usage notes

<menu><ul> 元素均表示项目的无序列表。主要区别在于 <ul> 主要包含用于显示的项目,而 <menu> 用于交互项目。相关的 <menuitem> 元素已被弃用。

¥The <menu> and <ul> elements both represent an unordered list of items. The key difference is that <ul> primarily contains items for display, while <menu> was intended for interactive items. The related <menuitem> element has been deprecated.

注意:在 HTML 规范的早期版本中,<menu> 元素有一个额外的用例作为上下文菜单。此功能被认为已过时,并且不在规范中。

¥Note: In early versions of the HTML specification, the <menu> element had an additional use case as a context menu. This functionality is considered obsolete and is not in the specification.

示例

¥Examples

工具栏

¥Toolbar

在此示例中,<menu> 用于创建编辑应用的工具栏。

¥In this example, a <menu> is used to create a toolbar for an editing application.

HTML

html
<menu>
  <li><button onclick="copy()">Copy</button></li>
  <li><button onclick="cut()">Cut</button></li>
  <li><button onclick="paste()">Paste</button></li>
</menu>

请注意,这在功能上与:

¥Note that this is functionally no different from:

html
<ul>
  <li><button onclick="copy()">Copy</button></li>
  <li><button onclick="cut()">Cut</button></li>
  <li><button onclick="paste()">Paste</button></li>
</ul>

CSS

css
menu,
ul {
  display: flex;
  list-style: none;
  padding: 0;
  width: 400px;
}

li {
  flex-grow: 1;
}

button {
  width: 100%;
}

结果

¥Result

技术总结

¥Technical summary

内容类别

流量内容.如果该元素的子元素至少包含一个 <li> 元素: 触手可及的内容

允许的内容

<li><script><template> 出现零次或多次。

标签遗漏 无,开始和结束标记都是强制性的。
允许的父级 任何接受 流动内容 的元素。
隐式 ARIA 角色 list
允许的 ARIA 角色 directorygrouplistboxmenumenubarnonepresentationradiogrouptablisttoolbartree
DOM 接口 HTMLMenuElement

规范

Specification
HTML Standard
# the-menu-element

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also

  • 其他与列表相关的 HTML 元素:<ol><ul><li>