Jquery的右键菜单插件ContextMenu

来源:互联网 发布:时光倒流软件 编辑:程序博客网 时间:2024/05/10 22:27
插件下载地址:
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/


插件中的参数说明:

[plain] view plaincopyprint?
  1. Parameters  
  2. menu_id  
  3. 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.   
  4. Note: This behaviour has changed from r1 where you needed a "#" before the id   
  5.   
  6. settings  
  7. 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:   
  8.   
  9. bindings   
  10. 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.   
  11. Note: This behaviour has changed from r1 where you needed a "#" before the id   
  12. menuStyle   
  13. An object containing styleName:value pairs for styling the containing <ul> menu.   
  14. itemStyle   
  15. An object containing styleName:value pairs for styling the <li> elements.   
  16. itemHoverStyle   
  17. An object containing styleName:value pairs for styling the hover behaviour of <li> elements.   
  18. shadow   
  19. Boolean: display a basic drop shadow on the menu.   
  20. Defaults to true   
  21. eventPosX   
  22. 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".   
  23. Defaults to: 'pageX'   
  24. eventPosY   
  25. 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".   
  26. Defaults to: 'pageY'   
  27. onContextMenu(event)   
  28. 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.   
  29. onShowMenu(event, menu)   
  30. 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.   
Parametersmenu_idThe 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 settingsContextMenu 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] view plaincopyprint?
  1. <HTML>  
  2.  <HEAD>  
  3.   <TITLE> JQuery右键菜单 </TITLE>  
  4.   <script  src="jquery-1.2.6.min.js"></script>  
  5.   <script src="jquery.contextmenu.r2.js"></script>  
  6.  </HEAD>  
  7.   
  8.  <BODY>  
  9.  <span class="demo1" style="color:green;">  
  10.     右键点此  
  11.  </span>  
  12. <hr />  
  13. <div id="demo2">  
  14.     右键点此  
  15. </div>  
  16. <hr />  
  17. <div class="demo3" id="dontShow">  
  18.   不显示  
  19. </div>  
  20. <hr />  
  21. <div class="demo3" id="showOne">  
  22.   显示第一项  
  23. </div>  
  24. <hr />  
  25. <div class="demo3" id="showAll">  
  26.   显示全部  
  27. </div>  
  28.   
  29. <hr />  
  30.     <!--右键菜单的源-->  
  31.      <div class="contextMenu" id="myMenu1">  
  32.       <ul>  
  33.         <li id="open"><img src="folder.png" /> 打开</li>  
  34.         <li id="email"><img src="email.png" /> 邮件</li>  
  35.         <li id="save"><img src="disk.png" /> 保存</li>  
  36.         <li id="delete"><img src="cross.png" /> 关闭</li>  
  37.       </ul>  
  38.     </div>  
  39.   
  40.     <div class="contextMenu" id="myMenu2">  
  41.         <ul>  
  42.           <li id="item_1">选项一</li>  
  43.           <li id="item_2">选项二</li>  
  44.           <li id="item_3">选项三</li>  
  45.           <li id="item_4">选项四</li>  
  46.         </ul>  
  47.    </div>  
  48.       
  49.      <div class="contextMenu" id="myMenu3">  
  50.          <ul>  
  51.           <li id="item_1">csdn</li>  
  52.           <li id="item_2">javaeye</li>  
  53.           <li id="item_3">itpub</li>  
  54.         </ul>  
  55.     </div>  
  56.  </BODY>  
  57.  <script>  
  58.     //所有class为demo1的span标签都会绑定此右键菜单  
  59.      $('span.demo1').contextMenu('myMenu1',   
  60.      {  
  61.           bindings:   
  62.           {  
  63.             'open': function(t) {  
  64.               alert('Trigger was '+t.id+'\nAction was Open');  
  65.             },  
  66.             'email': function(t) {  
  67.               alert('Trigger was '+t.id+'\nAction was Email');  
  68.             },  
  69.             'save': function(t) {  
  70.               alert('Trigger was '+t.id+'\nAction was Save');  
  71.             },  
  72.             'delete': function(t) {  
  73.               alert('Trigger was '+t.id+'\nAction was Delete');  
  74.             }  
  75.           }  
  76.   
  77.     });  
  78.     //所有html元素id为demo2的绑定此右键菜单  
  79.     $('#demo2').contextMenu('myMenu2', {  
  80.       //菜单样式  
  81.       menuStyle: {  
  82.         border: '2px solid #000'  
  83.       },  
  84.       //菜单项样式  
  85.       itemStyle: {  
  86.         fontFamily : 'verdana',  
  87.         backgroundColor : 'green',  
  88.         color: 'white',  
  89.         border: 'none',  
  90.         padding: '1px'  
  91.   
  92.       },  
  93.       //菜单项鼠标放在上面样式  
  94.       itemHoverStyle: {  
  95.         color: 'blue',  
  96.         backgroundColor: 'red',  
  97.         border: 'none'  
  98.       },  
  99.       //事件      
  100.       bindings:   
  101.           {  
  102.             'item_1': function(t) {  
  103.               alert('Trigger was '+t.id+'\nAction was item_1');  
  104.             },  
  105.             'item_2': function(t) {  
  106.               alert('Trigger was '+t.id+'\nAction was item_2');  
  107.             },  
  108.             'item_3': function(t) {  
  109.               alert('Trigger was '+t.id+'\nAction was item_3');  
  110.             },  
  111.             'item_4': function(t) {  
  112.               alert('Trigger was '+t.id+'\nAction was item_4');  
  113.             }  
  114.           }  
  115.     });  
  116.     //所有div标签class为demo3的绑定此右键菜单  
  117.     $('div.demo3').contextMenu('myMenu3', {  
  118.     //重写onContextMenu和onShowMenu事件  
  119.       onContextMenu: function(e) {  
  120.         if ($(e.target).attr('id') == 'dontShow') return false;  
  121.         else return true;  
  122.       },  
  123.   
  124.       onShowMenu: function(e, menu) {  
  125.         if ($(e.target).attr('id') == 'showOne') {  
  126.           $('#item_2, #item_3', menu).remove();  
  127.         }  
  128.         return menu;  
  129.       }  
  130.   
  131.     });  
  132.   
  133.   
  134.   
  135.  </script>  
  136. </HTML>  
<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>

效果图: