插件下载地址:
http://www.trendskitchens.co.nz/jquery/contextmenu/jquery.contextmenu.r2.js
压缩版:
http://www.trendskitchens.co.nz/jquery/contextmenu/jquery.contextmenu.r2.packed.js
Jquery主页: http://jquery.com/
插件中的参数说明:
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
Parameters
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
menu_id
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
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.
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
Note: This behaviour has changed from r1 where you needed a "#" before the id
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
settings
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
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:
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
bindings
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
An object containing "id":function pairs. The supplied function is the action to be performed when the associated item is clicked. The element that triggered the current menu is passed to this handler as the first parameter.
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
Note: This behaviour has changed from r1 where you needed a "#" before the id
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
menuStyle
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
An object containing styleName:value pairs for styling the containing <ul> menu.
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
itemStyle
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
An object containing styleName:value pairs for styling the <li> elements.
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
itemHoverStyle
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
An object containing styleName:value pairs for styling the hover behaviour of <li> elements.
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
shadow
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
Boolean: display a basic drop shadow on the menu.
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
Defaults to true
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
eventPosX
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
Allows you to define which click event is used to determine where to place the menu. There are possibly times (particularly in IE6) where you will need to set this to "clientX".
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
Defaults to: 'pageX'
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
eventPosY
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
Allows you to define which click event is used to determine where to place the menu. There are possibly times (particularly in IE6) where you will need to set this to "clientY".
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
Defaults to: 'pageY'
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
onContextMenu(event)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
A custom event function which runs before the context menu is displayed. If the function returns false the menu is not displayed. This allows you to attach the context menu to a large block element (or the entire document) and then filter on right click whether or not the context menu should be shown.
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
onShowMenu(event, menu)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
A custom event function which runs before the menu is displayed. It is passed a reference to the menu element and allows you to manipulate the output before the menu is shown. This allows you to hide/show options or anything else you can think of before showing the context menu to the user. This function must return the menu.
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
通过此插件可以在不同的html元素内建立contextmenu,并且可以自定义样式.
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<HTML>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<HEAD>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<TITLE> JQuery右键菜单 </TITLE>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<script src="jquery-1.2.6.min.js"></script>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<script src="jquery.contextmenu.r2.js"></script>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
</HEAD>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<BODY>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<span class="demo1" style="color:green;">
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
右键点此
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
</span>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<hr />
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<div id="demo2">
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
右键点此
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
</div>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<hr />
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<div class="demo3" id="dontShow">
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
不显示
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
</div>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<hr />
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<div class="demo3" id="showOne">
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
显示第一项
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
</div>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<hr />
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<div class="demo3" id="showAll">
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
显示全部
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
</div>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<hr />
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<!--右键菜单的源-->
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<div class="contextMenu" id="myMenu1">
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<ul>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<li id="open"><img src="folder.png" /> 打开</li>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<li id="email"><img src="email.png" /> 邮件</li>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<li id="save"><img src="disk.png" /> 保存</li>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<li id="delete"><img src="cross.png" /> 关闭</li>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
</ul>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
</div>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<div class="contextMenu" id="myMenu2">
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<ul>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<li id="item_1">选项一</li>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<li id="item_2">选项二</li>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<li id="item_3">选项三</li>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<li id="item_4">选项四</li>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
</ul>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
</div>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<div class="contextMenu" id="myMenu3">
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<ul>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<li id="item_1">csdn</li>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<li id="item_2">javaeye</li>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<li id="item_3">itpub</li>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
</ul>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
</div>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
</BODY>
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script>
//所有class为demo1的span标签都会绑定此右键菜单
$('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');
}
}
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
});
//所有html元素id为demo2的绑定此右键菜单
$('#demo2').contextMenu('myMenu2', {
//菜单样式
menuStyle: {
border: '2px solid #000'
},
//菜单项样式
itemStyle: {
fontFamily : 'verdana',
backgroundColor : 'green',
color: 'white',
border: 'none',
padding: '1px'
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
},
//菜单项鼠标放在上面样式
itemHoverStyle: {
color: 'blue',
backgroundColor: 'red',
border: 'none'
},
//事件
bindings:
{
'item_1': function(t) {
alert('Trigger was '+t.id+'\nAction was item_1');
},
'item_2': function(t) {
alert('Trigger was '+t.id+'\nAction was item_2');
},
'item_3': function(t) {
alert('Trigger was '+t.id+'\nAction was item_3');
},
'item_4': function(t) {
alert('Trigger was '+t.id+'\nAction was item_4');
}
}
});
//所有div标签class为demo3的绑定此右键菜单
$('div.demo3').contextMenu('myMenu3', {
//重写onContextMenu和onShowMenu事件
onContextMenu: function(e) {
if ($(e.target).attr('id') == 'dontShow') return false;
else return true;
},
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
onShowMenu: function(e, menu) {
if ($(e.target).attr('id') == 'showOne') {
$('#item_2, #item_3', menu).remove();
}
return menu;
}
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
});
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
</script>
</HTML>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
效果图:
![](http://www.blogjava.net/images/blogjava_net/supercrsky/jquery_contextmenu.JPG)