oncontextmenu
这个就是触发右键事件的

解决方案 »

  1.   

    给你看一个例子
    <html>    <body oncontextmenu = showMenu('')>        <form name = "menuForm">            <!--隐藏框,用来保存选择的菜单的id值-->            <input type = "hidden" name = "id" value = "">            <table>            <tr><td><a href="javascript:clickMenu()"  oncontextmenu = showMenu('0')>根目录</a></td></tr>            <tr><td><a href="javascript:clickMenu()"  oncontextmenu = showMenu('1')>菜单一</a></td></tr>            <tr><td><a href="javascript:clickMenu()"  oncontextmenu = showMenu('2')>菜单二</a></td></tr>            </table>        </form>    </body>        <!-- 这里用来定义需要显示的右键菜单 -->    <div id="itemMenu" style="display:none">           <table border="1" width="100%" height="100%" bgcolor="#cccccc" style="border:thin" cellspacing="0">                  <tr>                      <td style="cursor:default;border:outset 1;" align="center" onClick="parent.create()">                      新增                      </td>                  </tr>                  <tr>                      <td style="cursor:default;border:outset 1;" align="center" onClick="parent.update();">                      修改                      </td>                  </tr>                  <tr>                      <td style="cursor:default;border:outset 1;" align="center" onClick="parent.del()">                      删除                      </td>                  </tr>           </table>    </div>    <!-- 右键菜单结束--></html>
    <script language="JavaScript">/***根据传入的id显示右键菜单*/function showMenu(id){    menuForm.id.value = id;    if("" == id||id==0)    {        popMenu(itemMenu,100,"100");    }    else    {        popMenu(itemMenu,100,"011");    }    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!");}
    </script>
      

  2.   

    非常感谢你的回帖!给你贴我页面现在用的代码:<script>
    //创建popup窗口
    var oPopup = window.createPopup();
    var oDocument = oPopup.document;
    var oBody = oPopup.document.body;var root = false;
    var city = false;
    var lab = false;
    var classes = false;
    var project = false;
    var oPopMenu = {
    event : [],
    imgUrl : [],
    text : [],
    add : function(e, iu, t){
    this.event[this.event.length] = e;
    this.imgUrl[this.imgUrl.length] = iu;
    this.text[this.text.length] = t;
    },

    build : function(){
    var oPopBodyHTML = "";
    for(var i=0; i<this.text.length; i++){
    oPopBodyHTML += "<td class=MenuMouseOut onmouseover=this.className='MenuMouseOver' onmouseout=this.className='MenuMouseOut' onclick=\""+this.event[i]
    +"\"><img src=\""+this.imgUrl[i]+"\" align=left>&nbsp;"+this.text[i]+"</td></tr>";
    }
    return oPopBodyHTML;
    }}
    oBody.style.backgroundColor = "buttonface";
    oBody.style.borderTop = "1px buttonhighlight solid";
    oBody.style.borderLeft = "1px buttonhighlight solid";
    oBody.style.borderRight = "2px buttonshadow solid";
    oBody.style.borderBottom = "2px buttonshadow solid";
    oBody.style.padding = "1px";function showPop(id){
    addRtMenu(id);

    var bodyHTML = oPopMenu.build();
    var HTML = "<body oncontextmenu='return false'><style>"+
    ".MenuMouseOver{background-color:highlight;color: highlighttext;cursor: default;font-size:12px;line-height:20px;vertical-align:middle;padding-left:2px;}"+
    ".MenuMouseOut{background-color:buttonface;color:buttontext;cursor:default;font-size:12px;line-height:20px;vertical-align:middle;padding-left:2px;}"+
    ".btn{background-color:buttonface;cursor:default;height:1px;}"+
    "</style>";

    HTML+="<table onselectstart='return false' ondragstart='return false' oncontextmenu='return false' cellpadding=0 cellspacing=0 border=0 align=left>"+
    "<tr><td rowspan=3 bgcolor=#336699>&nbsp;&nbsp;</td>";
    HTML += bodyHTML;
    HTML += "</table></body>";
    HTML = HTML.replace(/id/g, id);
    oBody.innerHTML = HTML;
    var obj = event.srcElement;
    var iX = event.clientX;
    var iY = event.clientY;
    var iPopBodyHeight = oPopMenu.text.length * 20 + 4;
    oPopup.show(iX, iY, 128, iPopBodyHeight,document.body);

    }function addRtMenu(id){ if(id == 1 && root == false){
      // 清空右键菜单,如何写?
      oPopMenu.add("parent.doDiagEdit(id, 'new')", "images/insert.gif", "添加城市");
      root = true;
    }

    if(id > 1 && id < 999 && city == false){
      oPopMenu.add("parent.doDiagEdit(id, 'new')", "images/insert.gif", "添加实验室");
      oPopMenu.add("parent.doDiagEdit(id, 'edit')", "images/edit.gif", "修改城市");
          oPopMenu.add("parent.doDiagEdit(id, 'drop')", "images/delete.gif", "删除城市");
          city = true;
    }

    if(id > 999 && id < 99999 && lab == false){
      oPopMenu.add("parent.doDiagEdit(id, 'new')", "images/insert.gif", "添加班级");
      oPopMenu.add("parent.doDiagEdit(id, 'edit')", "images/edit.gif", "修改实验室");
          oPopMenu.add("parent.doDiagEdit(id, 'drop')", "images/delete.gif", "删除实验室");
          lab = true;
    }

    if(id > 99999 && id < 99999991231 && classes == false){
      oPopMenu.add("parent.doDiagEdit(id, 'new')", "images/insert.gif", "添加项目");
      oPopMenu.add("parent.doDiagEdit(id, 'edit')", "images/edit.gif", "修改班级");
          oPopMenu.add("parent.doDiagEdit(id, 'drop')", "images/delete.gif", "删除班级");
          classes = true;
    }

    if(id >99999991231 && id < 9999999123199 && project == false){
      oPopMenu.add("parent.doDiagEdit(id, 'edit')", "images/edit.gif", "修改项目");
          oPopMenu.add("parent.doDiagEdit(id, 'drop')", "images/delete.gif", "删除项目");
          project = true;
    }
    }function doDiagEdit(id,method){
    var arg = new Object();
    arg.id = id;
    arg.method = method;
    var win = window.showModalDialog("systemmanage/modify.jsp", arg, "dialogHeight:200px;dialogWidth:360px;scroll:no");
    }</script>
      

  3.   

    现在的问题是 你点了一个节点后,再去点另外的节点,刚才节点的右键菜单也跟着加到后面的节点菜单里去了。我对脚本不是很熟悉,我以为oPopMenu是个对象,我把这个对象设置为null,脚本出错。不知道怎么去写清空方法?我现在参考你的代码改一下看看,用隐藏试试。再次谢谢你!祝你身体健康,工作愉快!
      

  4.   

    将oPopMenu的3个属性全部置为空数组function addRtMenu(id){
        oPopMenu.event = new Array();
        oPopMenu.imgUrl= new Array();
        oPopMenu.text = new Array();    if(id == 1 && root == false){
          // 清空右键菜单,如何写?
          oPopMenu.add("parent.doDiagEdit(id, 'new')", "images/insert.gif", "添加城市");
          root = true;
        }
        
        if(id > 1 && id < 999 && city == false){
          oPopMenu.add("parent.doDiagEdit(id, 'new')", "images/insert.gif", "添加实验室");
          oPopMenu.add("parent.doDiagEdit(id, 'edit')", "images/edit.gif", "修改城市");
          oPopMenu.add("parent.doDiagEdit(id, 'drop')", "images/delete.gif", "删除城市");
          city = true;
        }
        
        if(id > 999 && id < 99999 && lab == false){
          oPopMenu.add("parent.doDiagEdit(id, 'new')", "images/insert.gif", "添加班级");
          oPopMenu.add("parent.doDiagEdit(id, 'edit')", "images/edit.gif", "修改实验室");
          oPopMenu.add("parent.doDiagEdit(id, 'drop')", "images/delete.gif", "删除实验室");
          lab = true;
        }
        
        if(id > 99999 && id < 99999991231 && classes == false){
          oPopMenu.add("parent.doDiagEdit(id, 'new')", "images/insert.gif", "添加项目");
          oPopMenu.add("parent.doDiagEdit(id, 'edit')", "images/edit.gif", "修改班级");
          oPopMenu.add("parent.doDiagEdit(id, 'drop')", "images/delete.gif", "删除班级");
          classes = true;
        }
        
        if(id >99999991231 && id < 9999999123199 && project == false){
          oPopMenu.add("parent.doDiagEdit(id, 'edit')", "images/edit.gif", "修改项目");
          oPopMenu.add("parent.doDiagEdit(id, 'drop')", "images/delete.gif", "删除项目");
          project = true;
        }
    }
      

  5.   

    大师 你太牛了!!!!!!!我对脚本不熟悉,昨天搞了一天没搞定,郁闷的不得了。你就加了三行代码我什么都不想说了,太强了!!看你头像,那是你跟你妻子吧,祝你们幸福哦!对了,我上午改了下你的脚本能满足我的要求了,我就用你的吧。谢谢你!PS:再请教个问题好吗?
    function create(){
      alert("create" + menuForm.id.value + "!");
    }
    我想在添加方法里面调用一个添加信息的页面显示到当前页面里面,我当前页面是个框架,用的是framesetfunction create(){
      window.open("添加信息页面",'ddd','目标框架');//这样写不行呢,老是打开一个新窗口
    }期待你的指点。
      

  6.   

    我已经解决,谢谢chinmo 和xuStanly 两位大师xuStanly 你好!不好意思,刚才错把你当成chinmo了。我上面那个代码就是参考你改动梅花雪树的大作。非常感谢!!祝周末愉快!我想每人给一百分不知道怎么操作