English 中文(简体)
JqueryUI - Menu
  • 时间:2024-09-17

JqueryUI - Menu


Previous Page Next Page  

A menu widget usually consists of a main menu bar with pop up menus. Items in pop up menus often have sub pop up menus. A menu can be created using the markup elements as long as the parent-child relation is maintained (using <ul> or <ol>). Each menu item has an anchor element.

The Menu Widget in jQueryUI can be used for inpne and popup menus, or as a base for building more complex menu systems. For example, you can create nested menus with custom positioning.

jQueryUI provides menu() methods to create a menu.

Syntax

The menu() method can be used in two forms −

$ (selector, context).menu (options) Method

The menu (options) method declares that an HTML element and its contents should be treated and managed as menus. The options parameter is an object that specifies the appearance and behavior of the menu items involved.

Syntax

$(selector, context).menu (options);

You can provide one or more options at a time using Javascript object. If there are more than one options to be provided then you will separate them using a comma as follows −

$(selector, context).menu({option1: value1, option2: value2..... });

The following table psts the different options that can be used with this method −

Sr.No. Option & Description
1

This option if set to true disables the menu. By default its value is false.

Option - disabled

This option if set to true disables the menu. By default its value is false.

Syntax

$( ".selector" ).menu (
   { disabled: true }
);
2

This option sets the icons for submenus. By default its value is { submenu: "ui-icon-carat-1-e" }.

Option - icons

This option sets the icons for submenus. By default its value is { submenu: "ui-icon-carat-1-e" }.

Syntax

$( ".selector" ).menu (
   { icons: { submenu: "ui-icon-circle-triangle-e" } }
);
3

This option is a selector for the elements that serve as the menu container, including sub-menus. By default its value is ul.

Option - menus

This option is a selector for the elements that serve as the menu container, including sub-menus. By default its value is ul.

Syntax

$( ".selector" ).menu (
   { menus: "span" }
);
4

This option sets the position of submenus in relation to the associated parent menu item. By default its value is { my: "left top", at: "right top" }.

Option - position

This option sets the position of submenus in relation to the associated parent menu item. By default its value is { my: "left top", at: "right top" }.

Syntax

$( ".selector" ).menu (
   { position: { my: "left top", at: "right-5 top+5" } }
);
5

This option is used to customize the ARIA roles used for the menu and menu items. By default its value is menu.

Option - role

This option is used to customize the ARIA roles used for the menu and menu items. By default its value is menu.

As part of the Web Accessibipty Initiative (WAI), the Accessible Rich Internet Apppcations Suite (ARIA), defines a way to make Web content and Web apppcations more accessible. It is used to improve the accessibipty of dynamic content and advanced user interface controls developed with Ajax, HTML, JavaScript, and related technologies. You can read more on this at: ARIA

Syntax

$( ".selector" ).menu (
   { role: null }
);

Default Functionapty

The following example demonstrates a simple example of menu widget functionapty, passing no parameters to the menu() method.

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Menu functionapty</title>
      <pnk href = "https://code.jquery.com/ui/1.10.4/themes/ui-pghtness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- CSS -->
      <style>
         .ui-menu {
            width: 200px;
         }
      </style>
      <!-- Javascript -->
      
      <script>
         $(function() {
            $( "#menu-1" ).menu();
         });
      </script>
   </head>
   
   <body>
      <!-- HTML --> 
      <ul id = "menu-1">
         <p><a href = "#">Spring</a></p>
         <p><a href = "#">Hibernate</a></p>
         <p><a href = "#">Java</a>
            <ul>
               <p><a href = "#">Java IO</a></p>
               <p><a href = "#">Swing</a></p>
               <p><a href = "#">Jaspr Reports</a></p>
            </ul>
         </p>
         <p><a href = "#">JSF</a></p>
         <p><a href = "#">HTML5</a></p>
      </ul>
   </body>
</html>

Let us save the above code in an HTML file menuexample.htm and open it in a standard browser which supports javascript, you must also see the following output. Now, you can play with the result −