Usage
$(elements).contextMenu(String menu_id [, Object settings]);
You define your menu structure in your HTML markup. For each menu, place an unordered list in a div with class "contextMenu" and the id you will refer to it by (see the examples). The div can be placed anywhere as it will automatically be hidden by the plugin.
You can have as many menus defined on a page as you like. Each <li> element will act as an option on the menu. Give each <li> a unique id so that actions can be bound to it.
Note: ContextMenu does not currently support nested menus. This feature may be in an upcoming release.
Parameters
menu_id
The id of the menu as defined in your markup. You can bind one or more elements to a menu. Eg $("table td").contextMenu("myMenu")
will bind the menu with id "myMenu" to all table cells.
Note: This behaviour has changed from r1 where you needed a "#" before the id
settings
ContextMenu takes an optional settings object that lets you style your menu and bind click handlers to each option. ContextMenu supports the following properties in the settings object:
Note: This behaviour has changed from r1 where you needed a "#" before the id
Defaults to true
Defaults to: 'pageX'
Defaults to: 'pageY'
Changing defaults
In addition to passing style information for each menu via the settings object, you can extend the default options for all menus by calling $.contextMenu.defaults(settings). Every setting except bindings can be used as a default.
Example:
$.contextMenu.defaults({ menuStyle : { border : "2px solid green" }, shadow: false, onContextMenu: function(e) { alert('Did someone asked for a context menu?!'); } });
Examples
Example 1 - Basic usage with bindings
RIGHT CLICK FOR DEMO THIS WORKS TOO
Html
<div class="contextMenu" id="myMenu1"> <ul> <li id="open"><img src="folder.png" /> Open</li> <li id="email"><img src="email.png" /> Email</li> <li id="save"><img src="disk.png" /> Save</li> <li id="close"><img src="cross.png" /> Close</li> </ul> </div>
Javascript
$('span.demo1').contextMenu('myMenu1', { bindings: { 'open': function(t) { alert('Trigger was '+t.id+'\nAction was Open'); }, 'email': function(t) { alert('Trigger was '+t.id+'\nAction was Email'); }, 'save': function(t) { alert('Trigger was '+t.id+'\nAction was Save'); }, 'delete': function(t) { alert('Trigger was '+t.id+'\nAction was Delete'); } } });
The preceding code binds the context menu "myMenu1" to all span elements of class "demo1".
Example 2 - Basic styling
Right clicking anywhere in this paragraph will trigger the context menu.
Html
<div class="contextMenu" id="myMenu2"> <ul> <li id="item_1">Item 1</li> <li id="item_2">Item 2</li> <li id="item_3">Item 3</li> <li id="item_4">Item 4</li> <!-- etc... --> </ul> </div>
Javascript
$('#demo2').contextMenu('myMenu2', { menuStyle: { border: '2px solid #000' }, itemStyle: { fontFamily : 'verdana', backgroundColor : '#666', color: 'white', border: 'none', padding: '1px' }, itemHoverStyle: { color: '#fff', backgroundColor: '#0f0', border: 'none' } });
The preceding code binds the context menu "myMenu2" to the element with id "demo2".
Example 3 - Advanced usage with callbacks
Don't show menu Just first item Show all
Html
<div class="contextMenu" id="myMenu3"> <ul> <li id="item_1">Item 1</li> <li id="item_2">Item 2</li> <li id="item_3">Item 3</li> </ul> </div>
Javascript
$('span.demo3').contextMenu('myMenu3', { onContextMenu: function(e) { if ($(e.target).attr('id') == 'dontShow') return false; else return true; }, onShowMenu: function(e, menu) { if ($(e.target).attr('id') == 'showOne') { $('#item_2, #item_3', menu).remove(); } return menu; } });
Notes
It is worth noting that by overriding the browser's right click menu this plugin provides a behaviour that is different from what most users will expect. This plugin may be best suited for intranet web applications, where the developer is working in a more controlled environment.
Credits
- A big thanks to John Resig for creating jQuery, and to the excellent development team who continues to make this the best javascript library in town.
- Thanks also to Joern Zaefferer - his Tooltip plugin provided great insight and inspiration.
- Dan G. Switzer, II, for his contributions
相关推荐
**jQuery右键菜单插件ContextJS详解** 在网页开发中,右键菜单通常是增强用户体验的一种常见功能,它能提供快捷的操作选项。`ContextJS`是一个专门为jQuery设计的右键菜单插件,允许开发者轻松地为网页元素添加...
《jQuery右键菜单插件context.js的深度解析与应用》 在前端开发中,交互体验是提升用户体验的关键一环,而右键菜单作为常见的交互元素,能够为用户提供便捷的操作选项。jQuery作为一个轻量级的JavaScript库,其丰富...
Jquery右键菜单插件(RightMenu)修正
**jQuery右键菜单插件——jquery-menu** `jquery-menu` 是一个基于jQuery的轻量级插件,专门用于创建右键或左键菜单。这个插件为开发者提供了一种便捷的方式来添加自定义的上下文菜单到网页的特定区域。在网页上,...
而 jQuery 的右键菜单插件则为用户提供了一种方便的方式来实现自定义的右键点击上下文菜单。在这个项目中,我们有四个关键文件:`contextmenufunction.js`、`jquery-1.4.2.js`、`jquery.contextmenu.css` 和 `jquery...
### 三、jQuery右键菜单插件的工作原理 1. **事件监听**:插件首先会监听鼠标右键点击事件,这通常是`contextmenu`事件。 2. **阻止默认行为**:当用户右键点击时,插件会阻止浏览器默认的右键菜单显示。 3. **动态...
用于创建右键弹出菜单的jQuery插件。 标签:jQuery
`jQuery-contextMenu`是一款强大的JavaScript插件,专为创建功能丰富的上下文菜单而设计,能够处理大量的元素。这款插件不仅支持传统的右键点击触发,还可以根据需求设置为左键点击显示菜单,极大地提升了用户的交互...
**jQuery右键菜单插件详解** 在Web开发中,用户交互是至关重要的,而右键菜单作为用户界面的一部分,能够提供便捷的操作入口。jQuery作为一款强大的JavaScript库,为开发者提供了丰富的插件,使得创建自定义右键...
而"jQuery右键菜单实现"是一个常见的需求,它允许用户在网页元素上右击鼠标时弹出一个自定义的菜单,提供额外的功能或选项。这个功能通常通过jQuery插件来实现,例如`jQuery contextMenu`。 `jQuery contextMenu`是...
**一、理解jQuery右键菜单的基本原理** jQuery右键菜单功能主要是通过监听鼠标右键点击事件(`contextmenu`)来触发自定义的菜单展示。当用户在网页上右键点击时,JavaScript会阻止浏览器默认的右键菜单弹出,然后...
总的来说,这款jQuery右键菜单插件提供了一种简单且灵活的方式来增强网站的交互性,使用户能够通过右键点击执行各种自定义操作。开发者可以根据需求调整菜单项,实现诸如复制、粘贴、删除等常见功能,也可以创建更...
Bootstrap Context Menu是一款非常实用的基于Bootstrap的jQuery右键上下文菜单插件。该右键菜单插件可以在多种元素上触发,也可以配合Font Awesome字体图标一起使用,非常的方便。
这是一款基于bootstrap的jquery右键菜单插件。该bootstrap右键菜单使用Bootstrap dropdown组件来制作,并通过tether插件进行定位,具有多级菜单,灵活,响应式等特点。
**jQuery右键菜单插件** 除了手动编写代码实现,还可以使用现成的jQuery插件来快速构建右键菜单,例如`jQuery.contextMenu`。这个插件提供了丰富的配置选项和回调函数,可以轻松地创建复杂或简单的右键菜单。使用...
关于右键菜单,我们介绍得并不多,目前只有介绍过一款jQuery Bootstrap右键菜单 带点击菜单...今天要继续为大家分享一款高大上的jQuery右键菜单,这款右键菜单可以让使用者自定义菜单弹出的区域,因此非常灵活和实用。