


Quickly 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/

HTMLtagsdefinethestructureofawebpage,whileattributesaddfunctionalityanddetails.1)Tagslike,,andoutlinethecontent'splacement.2)Attributessuchassrc,class,andstyleenhancetagsbyspecifyingimagesources,styling,andmore,improvingfunctionalityandappearance.

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
