使用<menu>元素作为导航是否正确?

huangapple go评论47阅读模式
英文:

Is it correct to use the <menu> element as navigation?

问题

使用<menu>标签作为导航是否正确?像这样:

<menu>
    <li><a href="#">Home</a></li>
    <li><a href="#">Page one</a></li>
    <li><a href="#">Page two</a></li>
    <li><a href="#">Page three</a></li>
</menu>

或者它仅用于上下文菜单,就像Mozilla上的示例一样?1: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/menu

英文:

Is it correct to use the &lt;menu&gt; tag as navigation?
Like that:

&lt;menu&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Page one&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Page two&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Page three&lt;/a&gt;&lt;/li&gt;
&lt;/menu&gt;

Or is it indicated only for a Context menu, like the example on Mozilla?

答案1

得分: 3

在你的示例中,你正确使用了&lt;menu&gt;标签,事实上这是你应该编写导航列表的方式。&lt;menu&gt;标签是对非语义化的&lt;ul&gt;的语义替代,浏览器会将它们视为相同。

根据MDN WebDocs的说法:

&lt;menu&gt;&lt;ul&gt; 元素都代表一个无序列表。主要区别在于 &lt;ul&gt; 主要包含用于显示的项目,而 &lt;menu&gt; 旨在用于交互项目。

&lt;a&gt; 锚点是交互元素,因此在这种情况下,应该正确使用 &lt;menu&gt; 作为列表容器。

然而,你的措辞是不正确的。&lt;menu&gt; 只是一个无序锚点列表的容器,而不是整个导航,应该使用 &lt;nav&gt; 元素。

例如,导航栏可能包含更多内容而不仅仅是链接:

&lt;nav&gt;
  &lt;img src=&quot;logo.jpg&quot; alt=&quot;网站Logo&quot;&gt;
  &lt;h1&gt;目录&lt;/h1&gt;
  &lt;menu&gt;
    &lt;li&gt;&lt;a href=&quot;#chapter-1&quot;&gt;第一章&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#chapter-2&quot;&gt;第二章&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#chapter-3&quot;&gt;第三章&lt;/a&gt;&lt;/li&gt;
  &lt;/menu&gt;
  &lt;input type=&quot;text&quot; id=&quot;search-bar&quot;&gt;
&lt;/nav&gt;
英文:

In your exmaple you are using the &lt;menu&gt; tag correctly and as matter of fact that is the way you should actually code a navigation list.<br>
The &lt;menu&gt; tag is a sementic alternative to the unsemantic &lt;ul&gt; as is treated by the browser as the same.

To quote MDN WebDocs:
> The &lt;menu&gt; and &lt;ul&gt; elements both represent an unordered list of items. The key difference is that &lt;ul&gt; primarily contains items for display, while &lt;menu&gt; was intended for interactive items.

&lt;a&gt;nchors are interactive elements and as such the &lt;menu&gt; should be correctly used as a list container in that case.

However, your wording is incorrect. &lt;menu&gt; is only a container for the unordered list of anchors but not an entire navigation for which the &lt;nav&gt; element should be used.

As example a navigation bar could contain more then just links:

&lt;nav&gt;
  &lt;img src=&quot;logo.jpg&quot; alt=&quot;Website Logo&quot;&gt;
  &lt;h1&gt;table of contents&lt;/h1&gt;
  &lt;menu&gt;
    &lt;li&gt;&lt;a href=&quot;#chapter-1&quot;&gt;Chapter 1&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#chapter-2&quot;&gt;Chapter 2&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#chapter-3&quot;&gt;Chapter 3&lt;/a&gt;&lt;/li&gt;
  &lt;/menu&gt;
  &lt;input type=&quot;text&quot; id=&quot;search-bar&quot;&gt;
&lt;/nav&gt;

答案2

得分: 2

根据规范:

> &lt;menu&gt; 元素 表示一个工具栏,由其内容组成,以无序列表的形式呈现(由 li 元素表示),其中每个元素表示用户可以执行或激活的命令。

这在工具栏中语义上是适当的。一个工具栏的示例是富文本格式菜单,用户可以按下“加粗”按钮以激活文本的加粗样式。

页面导航应使用 &lt;nav&gt; 元素 进行标记。如果您的导航是一组链接,则在该 &lt;nav&gt; 中适当使用 &lt;ul&gt;&lt;ol&gt;,而不是 &lt;menu&gt;

英文:

No. According to the spec:

> The &lt;menu&gt; element represents a toolbar consisting of its contents, in the form of an unordered list of items (represented by li elements), each of which represents a command that the user can perform or activate.

It's semantically appropriate for toolbars. One example of a toolbar is a rich text formatting menu, where users can press a "bold" button to activate bold styling on their text.

Page navigation should be marked up using a &lt;nav&gt; element. If your navigation is a list of links, use a &lt;ul&gt; or &lt;ol&gt; inside that &lt;nav&gt; as appropriate, but not a &lt;menu&gt;.

huangapple
  • 本文由 发表于 2023年5月25日 20:43:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定