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>

效果图:

时间: 2024-07-31 11:12:20

JQuery之ContextMenu(右键菜单)的相关文章

[xPlugins] jQuery Contextmenu右键菜单

[2012-04-12] Contextmenu 右键菜单 v0.1 版本发布 [功能] 在特定区域弹出右键菜单 [功能] 可以在弹出右键菜单区域内,再屏蔽某个小区域. [功能] 有两种方式添加右键菜单项,参数配置添加和指定ID项. [功能] 支持事件回调,有两种,一种以参数配置方式对应回调,指定ID项统一回调,可根据设置链接标签区别. [2012-04-29]Contextmenu 右键菜单 v0.2版本更新 [修正] 修正了指定ID添加内容在不存在时的bug. [新增] 新增了菜单失效后变成

3种不同的ContextMenu右键菜单演示

简单使用的右键菜单,希望能帮助大家.下面是截图和实例代码 实例预览 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>

给jquery easy-ui 添加右键菜单

版权声明:转自为EasyUI 的Tab 标签添加右键菜单 1 <!doctype html> 2 <html> 3 <head> 4 <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 5 <title>tabs 右键菜单demo QQ:15129679</title> 6 <link rel="

Jquery 右键菜单(ContextMenu)插件使用记录

目前做的项目需要在页面里面用右键菜单,在网上找到两种jquery的右键菜单插件,但是都有各种问题.所以就自己动手把两种插件结合了下. 修改后的右键菜单插架可以根据绑定的触发页面元素不同,复用同一个菜单使之根据触发页面元素有不同的行为.支持多个个触发页面元素复用同一个菜单时,分开禁用或恢复禁用菜单或某些菜单项目. 一些说明: 1.菜单的样式由css文件contextMenu.css决定,可以根据需要自行修改,请根据实际情况设定z-index的值,保证菜单在最高的一层 2.请将菜单直接放于body下

[xPlugin] jQuery右键菜单contextMenu实例

URL: http://www.cnblogs.com/whitewolf/archive/2011/09/28/2194795.html http://www.blogjava.net/supercrsky/articles/250091.html 好久没写博客了,今天简单介绍一款jQuery鼠标右键菜单contextMenu,这里首先要感谢 我的漫漫程序之旅(原文http://www.blogjava.net/supercrsky/articles/250091.html).在最近项目中需要

jQuery右键菜单contextMenu使用实例

在最近项目中需要频繁的右键菜单操作.我采用了contextMenu这款jQuery插件. 参考网址:http://www.jb51.net/article/58709.htm 官网demo http://medialize.github.io/jQuery-contextMenu/demo/callback.html 文章使用相关js文件 http://download.csdn.net/detail/chenxiang199055/6448645 在下面我们将设计一个场景,表格grid需要在每

jQuery EasyUI 右键菜单--关闭标签/选项卡

目录结构: noContextMenu.js 文件内容如下: $(function(){ //屏蔽右键菜单 $(document).bind("contextmenu", function(e){ return false; }); }); 效果图: 方式 一: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOC

jquery实现右键菜单

1 <!DOCTYPE html> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 4 <title>jQuery自定义区域的鼠标右键菜单</title> 5 <style type="text/css"> 6 #mask{position: absolute;l

【jQuery】smartMenu右键自定义上下文菜单插件(似web QQ)

一.这是什么样的一个插件 浏览器默认的右键选项有时候并不是我们所需要的,我们希望浏览器的右键选项菜单更智能,可以灵活自定义.比较有代表性的就是web QQ,例如下面截图: QQ邮箱中也是有此功能. 显然这种东西貌似还是蛮强大与实用的,于是我就抽空写了个可以右键自定义上下文菜单的jQuery插件 – smartMenu,直接一行代码绑定,就可以让我们轻松实现页面元素的自定义上下文功能.至于具体如何实用与绑定,就是本文的的主要内容,也即是下文即 将介绍的内容. 二.插件效果.大小.使用等简介 效果首