js实现右键菜单的一个实例

要求在tab上面增加一个右键菜单,里面包含两个选项:删除全部选项,删除其他选项。

html代码:

<div id="clickMenu" style="display:none; font-size:12px; background-color:white; border:2px solid rgb(246,244,240); list-style-type:none; border: node; text-align:left; width: 120px; border-bottom-color: 2px solid red;">
<ul  style="list-style-type:none; margin-top: 5px ; margin-left :-5px;border-color:2px splid #white; " >
    <li id = "divUl"><input type="button" value="关闭所有" onclick="removeAll()" style = "border:none; padding: 1px; margin-top: 10px; background-color: white;"/></li> 
    <hr style = "size: 1px; margin-top:10px;margin-top:10px;">
    <li id = "divUls" style = "magin-top:10px;"><input type="button" value="关闭其他" onclick="removeOther()" style = "border:none;padding: 1px; margin-top: -30px; background-color: white;"/></li>
    <li ><input id="hideInput" style="display:none;"/></li>
</ul>
</div>

js代码:

function mouseClick(){
    //屏蔽浏览器的右键
    document.body.onselectstart=document.body.oncontextmenu=function(){ return false;} 
    //绑定一个鼠标监听事件
    $("#my_tab").on("mousedown", "li", function(e) {
        //当鼠标右键的时候,e.which的值是3,左键点击和中间滑轮点击,自行百度
        if(3==e.which){
            //整体的思想是这样的:
            // 在页面 创建一个 div  id="clickMenu" 属性 display:none;
            // 包含2个按钮,新建两个点击事件  关闭全部(function),关闭其他(function)
            // <button onClick="removeOther(this)"></button>
            // <button onClick="removeAll(this)"></button>
            // 把 id="clickMenu" 的div 显示出来
            // 位置页面的最底部
            // 给这个 id="AA" 设置style
            mouseOut(e);
            //获取鼠标的位置
            //在火狐中不支持event函数,会报event没有定义的错误,所以我们初始化一下
            var event = arguments.callee.caller.arguments[0] || window.event;
            var xx = event.clientX;
            var yy = event.clientY;
            // 3个属性  绝对定位,top,left,根据鼠标所点击的位置设置
            $("#clickMenu").css({"position":"absolute","top":yy,"left":xx,"wight":"100px","hight":"100px"});
            //隐藏域传值         
            $("#hideInput").val(e.currentTarget.childNodes[0].attributes[0].nodeValue);
            //菜单显示出来
            $("#clickMenu").show();

        }
    });
}

因为我自己特殊的业务场景和框架,有iframe嵌套所以下面两个点击方法有点复杂。看看即可

function removeAll() {
     var tabContentId = $(".closeTab").parent().attr("href");
     $(".closeTab").parent().parent().remove(); //remove li of tab
     $('#my_tab a:last').tab('show'); // Select first tab
     $(tabContentId).remove(); //remove respective tab content
     $("#clickMenu").hide();
}

function removeOther() {
    var nod = $("#hideInput").val();
    var node = $(".closeTab");
    var nodes = node.parent().parent().children();
    var no = node.parent().parent().children().attr("href");
    for(var i = 0,nl = nodes.length;i<nl;i++){
        if($($(nodes)[i]).attr("href")!=nod){
            $($(nodes)[i]).parent().remove(); //remove li of tab
            $($($(nodes)[i]).attr("href")).remove(); //remove respective tab content
        } 
    }
    $("#clickMenu").hide();
    $(nod).show();
}

有问题的同仁,可私信(白天不回,你们懂的)。

根目录 菜单菜单二 go 修改 删除 function showMenu(id){ menuForm.id.value = id; if("" == id){ } else{ popMenu(itemMenu,100,"111"); } event.returnValue=false; event.cancelBubble=true; return false; } /** *显示弹出菜单 *menuDiv:右键菜单的内容 *width:行显示的宽度 *rowControlString:行控制字符串,0表示不显示,1表示显示,如“101”,则表示第1、3行显示,第2行不显示 */ function popMenu(menuDiv,width,rowControlString){ //创建弹出菜单 var pop=window.createPopup(); //设置弹出菜单的内容 pop.document.body.innerHTML=menuDiv.innerHTML; var rowObjs=pop.document.body.all[0].rows; //获得弹出菜单的行数 var rowCount=rowObjs.length; //循环设置每行的属性 for(var i=0;i<rowObjs.length;i++) { //如果设置该行不显示,则行数减一 var hide=rowControlString.charAt(i)!='1'; if(hide){ rowCount--; } //设置是否显示该行 rowObjs[i].style.display=(hide)?"none":""; //设置鼠标滑入该行时的效果 rowObjs[i].cells[0].onmouseover=function(){ this.style.background="#818181"; this.style.color="white"; } //设置鼠标滑出该行时的效果 rowObjs[i].cells[0].onmouseout=function(){ this.style.background="#cccccc"; this.style.color="black"; } } //屏蔽菜单菜单 pop.document.oncontextmenu=function(){ return false; } //选择右键菜单的一项后,菜单隐藏 pop.document.onclick=function(){ pop.hide(); } //显示菜单 pop.show(event.clientX-1,event.clientY,width,rowCount*25,document.body); return true; } function create(){ alert("create" + menuForm.id.value + "!"); } function update(){ alert("update" + menuForm.id.value + "!"); } function del(){ alert("delete" + menuForm.id.value + "!"); } function clickMenu(){ alert("you click a menu!"); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值