码迷,mamicode.com
首页 > Web开发 > 详细

JQuery之ContextMenu(右键菜单)

时间:2014-12-18 18:26:27      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   ar   io   color   os   sp   for   

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

插件中的参数说明:

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


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

bubuko.com,布布扣<HTML>
bubuko.com,布布扣 <HEAD>
bubuko.com,布布扣  <TITLE> JQuery右键菜单 </TITLE>
bubuko.com,布布扣  <script  src="jquery-1.2.6.min.js"></script>
bubuko.com,布布扣  <script src="jquery.contextmenu.r2.js"></script>
bubuko.com,布布扣 </HEAD>
bubuko.com,布布扣
bubuko.com,布布扣 <BODY>
bubuko.com,布布扣 <span class="demo1" style="color:green;">
bubuko.com,布布扣    右键点此
bubuko.com,布布扣 </span>
bubuko.com,布布扣<hr />
bubuko.com,布布扣<div id="demo2">
bubuko.com,布布扣    右键点此
bubuko.com,布布扣</div>
bubuko.com,布布扣<hr />
bubuko.com,布布扣<div class="demo3" id="dontShow">
bubuko.com,布布扣  不显示
bubuko.com,布布扣</div>
bubuko.com,布布扣<hr />
bubuko.com,布布扣<div class="demo3" id="showOne">
bubuko.com,布布扣  显示第一项
bubuko.com,布布扣</div>
bubuko.com,布布扣<hr />
bubuko.com,布布扣<div class="demo3" id="showAll">
bubuko.com,布布扣  显示全部
bubuko.com,布布扣</div>
bubuko.com,布布扣
bubuko.com,布布扣<hr />
bubuko.com,布布扣    <!--右键菜单的源-->
bubuko.com,布布扣     <div class="contextMenu" id="myMenu1">
bubuko.com,布布扣      <ul>
bubuko.com,布布扣        <li id="open"><img src="folder.png" /> 打开</li>
bubuko.com,布布扣        <li id="email"><img src="email.png" /> 邮件</li>
bubuko.com,布布扣        <li id="save"><img src="disk.png" /> 保存</li>
bubuko.com,布布扣        <li id="delete"><img src="cross.png" /> 关闭</li>
bubuko.com,布布扣      </ul>
bubuko.com,布布扣    </div>
bubuko.com,布布扣
bubuko.com,布布扣    <div class="contextMenu" id="myMenu2">
bubuko.com,布布扣        <ul>
bubuko.com,布布扣          <li id="item_1">选项一</li>
bubuko.com,布布扣          <li id="item_2">选项二</li>
bubuko.com,布布扣          <li id="item_3">选项三</li>
bubuko.com,布布扣          <li id="item_4">选项四</li>
bubuko.com,布布扣        </ul>
bubuko.com,布布扣   </div>
bubuko.com,布布扣    
bubuko.com,布布扣     <div class="contextMenu" id="myMenu3">
bubuko.com,布布扣         <ul>
bubuko.com,布布扣          <li id="item_1">csdn</li>
bubuko.com,布布扣          <li id="item_2">javaeye</li>
bubuko.com,布布扣          <li id="item_3">itpub</li>
bubuko.com,布布扣        </ul>
bubuko.com,布布扣    </div>
bubuko.com,布布扣 </BODY>
bubuko.com,布布扣 <script>
bubuko.com,布布扣    //所有class为demo1的span标签都会绑定此右键菜单
bubuko.com,布布扣     $(‘span.demo1‘).contextMenu(‘myMenu1‘, 
bubuko.com,布布扣     {
bubuko.com,布布扣          bindings: 
bubuko.com,布布扣          {
bubuko.com,布布扣            ‘open‘: function(t) {
bubuko.com,布布扣              alert(‘Trigger was ‘+t.id+‘\nAction was Open‘);
bubuko.com,布布扣            },
bubuko.com,布布扣            ‘email‘: function(t) {
bubuko.com,布布扣              alert(‘Trigger was ‘+t.id+‘\nAction was Email‘);
bubuko.com,布布扣            },
bubuko.com,布布扣            ‘save‘: function(t) {
bubuko.com,布布扣              alert(‘Trigger was ‘+t.id+‘\nAction was Save‘);
bubuko.com,布布扣            },
bubuko.com,布布扣            ‘delete‘: function(t) {
bubuko.com,布布扣              alert(‘Trigger was ‘+t.id+‘\nAction was Delete‘);
bubuko.com,布布扣            }
bubuko.com,布布扣          }
bubuko.com,布布扣
bubuko.com,布布扣    });
bubuko.com,布布扣    //所有html元素id为demo2的绑定此右键菜单
bubuko.com,布布扣    $(‘#demo2‘).contextMenu(‘myMenu2‘, {
bubuko.com,布布扣      //菜单样式
bubuko.com,布布扣      menuStyle: {
bubuko.com,布布扣        border: ‘2px solid #000‘
bubuko.com,布布扣      },
bubuko.com,布布扣      //菜单项样式
bubuko.com,布布扣      itemStyle: {
bubuko.com,布布扣        fontFamily : ‘verdana‘,
bubuko.com,布布扣        backgroundColor : ‘green‘,
bubuko.com,布布扣        color: ‘white‘,
bubuko.com,布布扣        border: ‘none‘,
bubuko.com,布布扣        padding: ‘1px‘
bubuko.com,布布扣
bubuko.com,布布扣      },
bubuko.com,布布扣      //菜单项鼠标放在上面样式
bubuko.com,布布扣      itemHoverStyle: {
bubuko.com,布布扣        color: ‘blue‘,
bubuko.com,布布扣        backgroundColor: ‘red‘,
bubuko.com,布布扣        border: ‘none‘
bubuko.com,布布扣      },
bubuko.com,布布扣      //事件    
bubuko.com,布布扣      bindings: 
bubuko.com,布布扣          {
bubuko.com,布布扣            ‘item_1‘: function(t) {
bubuko.com,布布扣              alert(‘Trigger was ‘+t.id+‘\nAction was item_1‘);
bubuko.com,布布扣            },
bubuko.com,布布扣            ‘item_2‘: function(t) {
bubuko.com,布布扣              alert(‘Trigger was ‘+t.id+‘\nAction was item_2‘);
bubuko.com,布布扣            },
bubuko.com,布布扣            ‘item_3‘: function(t) {
bubuko.com,布布扣              alert(‘Trigger was ‘+t.id+‘\nAction was item_3‘);
bubuko.com,布布扣            },
bubuko.com,布布扣            ‘item_4‘: function(t) {
bubuko.com,布布扣              alert(‘Trigger was ‘+t.id+‘\nAction was item_4‘);
bubuko.com,布布扣            }
bubuko.com,布布扣          }
bubuko.com,布布扣    });
bubuko.com,布布扣    //所有div标签class为demo3的绑定此右键菜单
bubuko.com,布布扣    $(‘div.demo3‘).contextMenu(‘myMenu3‘, {
bubuko.com,布布扣    //重写onContextMenu和onShowMenu事件
bubuko.com,布布扣      onContextMenu: function(e) {
bubuko.com,布布扣        if ($(e.target).attr(‘id‘) == ‘dontShow‘) return false;
bubuko.com,布布扣        else return true;
bubuko.com,布布扣      },
bubuko.com,布布扣
bubuko.com,布布扣      onShowMenu: function(e, menu) {
bubuko.com,布布扣        if ($(e.target).attr(‘id‘) == ‘showOne‘) {
bubuko.com,布布扣          $(‘#item_2, #item_3‘, menu).remove();
bubuko.com,布布扣        }
bubuko.com,布布扣        return menu;
bubuko.com,布布扣      }
bubuko.com,布布扣
bubuko.com,布布扣    });
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣 </script>
bubuko.com,布布扣</HTML>
bubuko.com,布布扣

效果图:
bubuko.com,布布扣

JQuery之ContextMenu(右键菜单)

标签:style   blog   http   ar   io   color   os   sp   for   

原文地址:http://www.cnblogs.com/ranzige/p/4172273.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!