search
HomeWeb Front-endHTML TutorialQuickly create menus using the Menu and Menuitem elements in HTML 5_html/css_WEB-ITnose

Translation: Introduction to the elements of Menu and Menuitem in HTML 5

Translator: dwqs

Today I will introduce to you the elements of Menu and Menuitem in HTML 5 Two elements: Menu and Menuitem, these two elements are part of the W3C interactive elements. Now the evolution of the Web is not limited to links between documents. In APPs, pages have more and more behaviors. Therefore, it is time to form a standard for Web interaction.

Menu and Menuiitem are the two most talked about elements among developers, probably due to the lack of sufficient support for them by mainstream browsers. As I write this article, FireFox has implemented this element.

Comparison between Menu and Nav

When it comes to Menu, it is inevitable not to be confused with the Nav element. To differentiate between these two elements, document specifications are a good tool.

Nav element is an HTML navigation element, which represents the navigation block of a Web page. It generally contains a collection of links, allowing users to jump within the page or jump to other website pages.

Menu is a collection of menu commands, somewhat similar to desktop or mobile applications. Desktop applications typically use toolbar menus or context menus to present various tasks. The most fundamental difference between Nav and Menu is that the Nav element contains navigation links to help users jump between Web pages, while Menu should allow users to perform specific tasks.

Using the Menu element

In a container, the Menu element is used to create context, toolbars and pop-up menus. However, the latter two features have not yet been implemented in browsers, including FireFox. At the moment, it's hard to guess how browsing will implement them and what it will look like. However, it is also a good opportunity to make some changes to the specifications of toolbar and popup menu in the next generation of interaction design.

For now, we'll focus on context.

Context

When we right-click an application, a context menu will appear. The options shown depend on where the user clicks.

JavaScript Options

Adding context menus on web pages is possible through JavaScript and JQuery plugins. The problem is that this approach requires additional markup and the script removes the browser's native menu, which if not handled properly will frustrate the user.

Local solution

Menu and Menuitem are used together to merge the new menu into the local context menu. For example, add a menu called "Hello World" to the body

<body contextmenu="new-context-menu"><menu id="new-context-menu" type="context">    <menuitem>Hello World</menuitem></menu></body>

In the above code snippet, the basic attributes included are id, type and contextmenu ?It specifies the menu type as context, and also specifies the area where the new menu item should be displayed.

In the example, when the mouse is right-clicked, the new menu item will appear anywhere in the document because we specified that its scope is the body.

Of course, you can limit the scope of the new menu item by assigning a value to contextmenu on specific elements, such as div, main, section, etc.

<body>    <div contextmenu="new-context-menu">    <!-- content -->    </div>    <menu id="new-context-menu" type="context">        <menuitem>Hello World</menuitem>    </menu></body>

When viewed in FireFox, you will find that the newly added menu item is added to the top.

Add submenus and icons

A submenu consists of a group of similar or mutual menu items. Image Rotation in PS is a typical example. Adding submenus using Menu is very easy and intuitive. Check out the sample code below:

<menu id="demo-image" type="context">    <menu label="Image Rotation">        <menuitem>Rotate 90</menuitem>        <menuitem>Rotate 180</menuitem>        <menuitem>Flip Horizontally</menuitem>        <menuitem>Flip Vertically</menuitem>    </menu></menu>

When run in a browser that supports the Menu element, you will see four submenus added to the new menu:

Icon

Introducing a new attribute: icon, use this attribute to add an icon next to the menu. It is worth mentioning that the icon attribute can only be used in menuitem elements. Sample code:

<menu id="demo-image" type="context">    <menu label="Image Rotation">        <menuitem icon="img/arrow-return-090.png">Rotate 90</menuitem>        <menuitem icon="img/arrow-return-180.png">Rotate 180</menuitem>        <menuitem icon="img/arrow-stop-180.png">Flip Horizontally</menuitem>        <menuitem icon="img/arrow-stop-270.png">Flip Vertically</menuitem>    </menu></menu>

The result is as you can see:

Add function in menu

We have built some examples that look like menus, but without any functionality. When clicking on a menu, users expect something to happen. For example, clicking Copy should copy text or links, and clicking New Folder should create a new folder. These functions can be implemented using JavaScript.

Note: Before you get started, I recommend checking out Jeremy McPeak’s course at JavaScript Fundamentals, which is a great place to start for anyone who wants to learn JavaScript.

Using the “Image Rotation” example above, let’s add a function that rotates the image when clicked. CSS 3's Transform and Transition can implement this function for us in the browser. The style that rotates an image 90 degrees is as follows:

.rotate-90 {    transform: rotate(90deg);}

In order to use this style, you need to write a function to apply it to the image.

function imageRotation(name) {    document.getElementById('image').className = name;}

把这个函数和每一个menuitem的onclick属性关联,并且传递一个参数:rotate-90

<menu id="demo-image" type="context">    <menu label="Image Rotation">        <menuitem onclick="imageRotation('rotate-90')" icon="img/arrow-return-090.png">Rotate 90</menuitem>        <menuitem icon="img/arrow-return-180.png">Rotate 180</menuitem>        <menuitem icon="img/arrow-stop-180.png">Flip Horizontally</menuitem>        <menuitem icon="img/arrow-stop-270.png">Flip Vertically</menuitem>    </menu></menu>

 

完成这个之后,再创建将图片旋转180度和裁剪图片的样式,将每一个函数添加到独立的menuitem中,记得传参数 。查看效果:in the demo page.

查看更多关于Menu元素的信息:Interactive Element: The Menu Element

查看我的示例:http://jsfiddle.net/Web_Code/15pc5zfv/1/embedded/result/

 

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS, and JavaScript: Essential Tools for Web DevelopersHTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

The Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesThe Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesApr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Is HTML easy to learn for beginners?Is HTML easy to learn for beginners?Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

What is an example of a starting tag in HTML?What is an example of a starting tag in HTML?Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

How to use CSS's Flexbox layout to achieve centering alignment of dotted line segmentation effect in menu?How to use CSS's Flexbox layout to achieve centering alignment of dotted line segmentation effect in menu?Apr 05, 2025 pm 01:24 PM

How to design the dotted line segmentation effect in the menu? When designing menus, it is usually not difficult to align left and right between the dish name and price, but how about the dotted line or point in the middle...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function