JS右鍵點擊事件

來源:互聯網
上載者:User

<%--

/**

*實現右鍵菜單功能

*/

--%>

<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)

    {

        popMenu(itemMenu,100,"100");

    }

    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":"";

        //設定滑鼠滑入該行時的$#¥,·#t\u0007U\u0010H\u000Ea\u001Ei效果

        rowObjs[i].cells[0].onmouseover=function()

        {

            this.style.background="#818181";

            this.style.color="white";

        }

        //設定滑鼠滑出該行時的$#¥,·#t\u0007U\u0010H\u000Ea\u001Ei效果

        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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.