我走在乡间的小路上 2015-01-12 03:33 采纳率: 0%
浏览 2719

js应该如何编写右击事件

我想实现鼠标右击时,触发某个事件.
应该怎么做呢,求具体代码!

  • 写回答

5条回答 默认 最新

  • WorldMobile 2015-01-12 03:37
    关注

    非常简洁高效的JS右键菜单

    在制作网页的时候,我们很经常需要用到右键菜单,网上这类的控件很多,很眩. 但这类控件一般都比较大,需要写的代码比较多,而且样式比较固定. 而本组件相对简单高效, 定制性很强, 菜单全由自己DIY!

    function csMenu(_object, _menu)
    {
    this.IEventHander = null;
    this.IFrameHander = null;
    this.IContextMenuHander = null;

    this.Show = function(_menu)
    {
        var e = window.event || event;
        if (e.button == 2)
        {
            if (window.document.all)
            {
                this.IContextMenuHander = function(){return false;};
                document.attachEvent("oncontextmenu", this.IContextMenuHander);
            }
            else
            {
                this.IContextMenuHander = document.oncontextmenu;
                document.oncontextmenu = function(){return false;};
            }
    
            window.csMenu$Object = this;
            this.IEventHander = function(){window.csMenu$Object.Hide(_menu);};
    
            if (window.document.all)
                document.attachEvent("onmousedown", this.IEventHander);
            else
                document.addEventListener("mousedown", this.IEventHander, false);
    
            _menu.style.left = e.clientX;
            _menu.style.top = e.clientY;
            _menu.style.display = "";
    
            if (this.IFrameHander)
            {
                var _iframe = document.getElementById(this.IFrameHander);
                _iframe.style.left = e.clientX;
                _iframe.style.top = e.clientY;
                _iframe.style.height = _menu.offsetHeight;
                _iframe.style.width = _menu.offsetWidth;
                _iframe.style.display = "";
            }
        }
    };
    
    this.Hide = function(_menu)
    {
        var e = window.event || event;
        var _element = e.srcElement;
        do
        {
            if (_element == _menu)
            {
                return false;
            }
        }
        while ((_element = _element.offsetParent));
    
        if (window.document.all)
         document.detachEvent("on"+e.type, this.IEventHander);
        else
         document.removeEventListener(e.type, this.IEventHander, false);
    
        if (this.IFrameHander)
        {
            var _iframe = document.getElementById(this.IFrameHander);
            _iframe.style.display = "none";
        }
    
        _menu.style.display = "none";
    
        if (window.document.all)
         document.detachEvent("oncontextmenu", this.IContextMenuHander);
        else
         document.oncontextmenu = this.IContextMenuHander;
    };
    
    this.initialize = function(_object, _menu)
    {  
        window._csMenu$Object = this;
        var _eventHander = function(){window._csMenu$Object.Show(_menu);};
    
        _menu.style.position = "absolute";
     _menu.style.display = "none";
     _menu.style.zIndex = "1000000";
    
        if (window.document.all)
        {
            var _iframe = document.createElement('iframe');
     document.body.insertBefore(_iframe, document.body.firstChild);
            _iframe.id = _menu.id + "_iframe";
            this.IFrameHander = _iframe.id;
    
            _iframe.style.position = "absolute";
            _iframe.style.display = "none";
            _iframe.style.zIndex = "999999";
            _iframe.style.border = "0px";
            _iframe.style.height = "0px";
            _iframe.style.width = "0px";
    
            _object.attachEvent("onmouseup", _eventHander);
        }
        else
        {
            _object.addEventListener("mouseup", _eventHander, false);
        }
    };
    
    this.initialize(_object, _menu);
    

    }

    =======================需要显示的菜单,一般用DIV======================

    打开 打印 回复发件人 全部回复 转发 分配 垃圾邮件 删除 归档此邮件 分拣此邮件

    ============================调用方法===============================

    var MM = new csMenu(document.getElementById("Table1"), document.getElementById("Menu1"));

    其中, document.getElementById("Table1") 就是需要显示菜单的区域控件.
    document.getElementById("Menu1") 就是菜单DIV控件.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向