zoukankan      html  css  js  c++  java
  • 自定义右键菜单

    代码如下:

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
            <style>
                html, body {
                    margin: 0;
                    padding: 0;
                }
                ul {
                     40px;
                    list-style: none;
                    display: inline-block;
                    margin: 0;
                    padding: 0;
                    position: fixed;
                }
                ul li {
                    padding-left: 10px;
                    line-height: 20px;
                    background: greenyellow;
                }
            </style>
        </head>
        <body>
            <ul id="customList">
                <li>11</li>
                <li>22</li>
                <li>33</li>
            </ul>
        </body>
        <script>
            document.oncontextmenu= function(event){
                var e= event || window.event;
                e.preventDefault? e.preventDefault():e.returnValue= false;
                var customList= document.getElementById("customList");
                customList.style.left= e.clientX+ "px";
                customList.style.top= e.clientY+ "px";
            }
        </script>
    </html>

    解析:

    document.oncontextmenu: oncontextmenu方法在目标元素上右键时触发,兼容性良好

    var e= event || window.event;
    e.preventDefault? e.preventDefault():e.returnValue= false;

    兼容IE并阻止默认行为

    customList.style.left= e.clientX+ "px";
    customList.style.top= e.clientY+ "px";

    定位自定义的菜单

  • 相关阅读:
    12-29 批量删除
    12-29 注册审核
    12-25造数据库面向对象
    12-23 会话保持
    2016-12-19 php修改数据库数据
    12-18数据访问
    12-16php测试题
    1027 制作表格
    1027 超链接
    1027 HTML
  • 原文地址:https://www.cnblogs.com/yanze/p/5990135.html
Copyright © 2011-2022 走看看