我的漫漫程序之旅

专注于JavaWeb开发
随笔 - 39, 文章 - 310, 评论 - 411, 引用 - 0
数据加载中……

JQuery之ContextMenu(右键菜单)

 

插件下载地址:
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/

插件中的参数说明:
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: 

bindings 
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. 
Note: This behaviour has changed from r1 where you needed a "#" before the id 
menuStyle 
An object containing styleName:value pairs for styling the containing 
<ul> menu. 
itemStyle 
An object containing styleName:value pairs for styling the 
<li> elements. 
itemHoverStyle 
An object containing styleName:value pairs for styling the hover behaviour of 
<li> elements. 
shadow 
Boolean: display a basic drop shadow on the menu. 
Defaults to true 
eventPosX 
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". 
Defaults to: 'pageX' 
eventPosY 
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". 
Defaults to: 'pageY' 
onContextMenu(event) 
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. 
onShowMenu(event, menu) 
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. 

通过此插件可以在不同的html元素内建立contextmenu,并且可以自定义样式.

<HTML>
 
<HEAD>
  
<TITLE> JQuery右键菜单 </TITLE>
  
<script  src="jquery-1.2.6.min.js"></script>
  
<script src="jquery.contextmenu.r2.js"></script>
 
</HEAD>

 
<BODY>
 
<span class="demo1" style="color:green;">
    右键点此
 
</span>
<hr />
<div id="demo2">
    右键点此
</div>
<hr />
<div class="demo3" id="dontShow">
  不显示
</div>
<hr />
<div class="demo3" id="showOne">
  显示第一项
</div>
<hr />
<div class="demo3" id="showAll">
  显示全部
</div>

<hr />
    
<!--右键菜单的源-->
     
<div class="contextMenu" id="myMenu1">
      
<ul>
        
<li id="open"><img src="folder.png" /> 打开</li>
        
<li id="email"><img src="email.png" /> 邮件</li>
        
<li id="save"><img src="disk.png" /> 保存</li>
        
<li id="delete"><img src="cross.png" /> 关闭</li>
      
</ul>
    
</div>

    
<div class="contextMenu" id="myMenu2">
        
<ul>
          
<li id="item_1">选项一</li>
          
<li id="item_2">选项二</li>
          
<li id="item_3">选项三</li>
          
<li id="item_4">选项四</li>
        
</ul>
   
</div>
    
     
<div class="contextMenu" id="myMenu3">
         
<ul>
          
<li id="item_1">csdn</li>
          
<li id="item_2">javaeye</li>
          
<li id="item_3">itpub</li>
        
</ul>
    
</div>
 
</BODY>
 
<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');
            }

          }


    }
);
    
//所有html元素id为demo2的绑定此右键菜单
    $('#demo2').contextMenu('myMenu2', {
      
//菜单样式
      menuStyle: {
        border: '2px solid #
000'
      }
,
      
//菜单项样式
      itemStyle: {
        fontFamily : 'verdana',
        backgroundColor : 'green',
        color: 'white',
        border: 'none',
        padding: '1px'

      }
,
      
//菜单项鼠标放在上面样式
      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;
      }
,

      onShowMenu: 
function(e, menu) {
        
if ($(e.target).attr('id') == 'showOne') {
          $('#item_2, #item_3', menu).remove();
        }

        
return menu;
      }


    }
);



 
</script>
</HTML>
效果图:


posted on 2009-01-06 14:11 々上善若水々 阅读(66445) 评论(10)  编辑  收藏 所属分类: JQuery

评论

# re: JQuery之ContextMenu(右键菜单)  回复  更多评论   

楼主你好,能帮忙指出代码中哪里判断的点击的是右键 吗。
2009-05-22 10:30 | sc

# re: JQuery之ContextMenu(右键菜单)  回复  更多评论   

onContextMenu: function(e) {
if ($(e.target).attr('id') == 'dontShow') return false;
else return true;
},
2009-05-25 11:48 | supercrsky

# re: JQuery之ContextMenu(右键菜单)  回复  更多评论   

楼主你好:
能否解释一下这句的意思:
$('#'+id, menu).bind('click', function(e)
如果我不使用xpath方式,用css方式应该怎么样谢谢,
2009-11-11 05:57 | river

# re: JQuery之ContextMenu(右键菜单)[未登录]  回复  更多评论   

可否说下怎样实现,含字菜单
2010-09-28 17:21 | calvin

# re: JQuery之ContextMenu(右键菜单)  回复  更多评论   

你好, $("td").livequery(function (e) {
var td = $(this);

$(this).contextMenu('myMenu1', {

bindings: {

'factory': function (t) {



},

'email': function (t) {

alert('Trigger was ' + $("#hide1").val() + '\nAction was Email');
location.href = "location.aspx?trid=" + $("#hide1").val();

},

'save': function (t) {

alert('Trigger was ' + t.id + '\nAction was Save');

},

'inv': function (t) {

alert('Trigger was ' + t.id + '\nAction was Delete');

},

'insert': function (t) {

var a = "<tr><td id='a'></td><td id='b'></td><td></td><td></td><td></td></tr>"
$("tbody").append(a);

},

'delete': function (t) {

alert(t.id);
alert($("#hide1").val());

},

'color': function (t) {

var offset = td.offset();


$("#colorselections").css({
"top": offset.top + "px",
"left": offset.left + "px"
}).show();
$("#colorselections a").click(function () {
$(this).addClass("on").parent().siblings().children().removeClass("on");
alert(td.attr("id"));
$("#" + td.attr("id")).css("color", $(this).css("background-color"));

});

}









}//binding

}); //contextMenu

});
这是我写的程序,我想改变td中字体的颜色,可是我改变了一个td的字体颜色,然后再改变另一个td的颜色,结果所有的td的颜色都变成一样的了。
2010-12-13 10:34 | wtq

# re: JQuery之ContextMenu(右键菜单)  回复  更多评论   

真历害,非常感谢师傅
2010-12-13 21:18 | 小王

# re: JQuery之ContextMenu(右键菜单)  回复  更多评论   

问一下就是在右击的时候同时改变div的背景色怎么办
2010-12-16 10:33 | re

# re: JQuery之ContextMenu(右键菜单)  回复  更多评论   

楼主,当屏蔽右键菜单中的一个菜单时,菜单没有办法操作了,是contextMenu的bug吗?要如何解决呀?
2011-05-19 10:42 | linian

# re: JQuery之ContextMenu(右键菜单)  回复  更多评论   

右键菜单过长时,在稍微靠下位置会撑开页面,应该怎么解决
?谢谢
2012-01-16 15:56 | ai

# re: JQuery之ContextMenu(右键菜单)  回复  更多评论   

我很急,请帮忙解决一下,谢了!!!
2012-01-16 15:56 | ai

只有注册用户登录后才能发表评论。


网站导航: