JS - 实现右键菜单功能

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        #youjiancaidan{
            border: 1px solid #ccc;
            padding: 10px 2px;
            position: absolute;
            background: #e4e4e4;
            display: none;
        }
        #youjiancaidan ul{
            list-style: none;
            margin: 0;
            padding: 0;
        }
        #youjiancaidan ul li{
            padding: 5px;
            transition: all 0.2s;
        }
        #youjiancaidan li:hover{
            color: #fff;
            background: #1c00ff;
            cursor: pointer;
        }
    </style>
</head>
<body>


<!--(1)菜单选项一、弹出alert提示框,内容自拟
(2)菜单选项二、提示用户是否离开本页面。
(3)菜单选项三、跳转至百度搜索【页面中选中的内容】
(4)菜单选项四、弹出提示框,用户【在提示框中输入内容】然后跳转至百度进行搜索-->




<textarea name="" id="" cols="100" rows="30"></textarea>


<div id="youjiancaidan">
    <ul>
        <li>咱俩去吃麻辣烫吧,6元一碗的那种</li>
        <li>是否离开本页面</li>
        <li>将页面中选中的内容去百度搜索</li>
        <li>输入内容,然后去百度搜索</li>
    </ul>
</div>


<script>
    var youjiancaidan = document.querySelector('#youjiancaidan');
    //禁用系统右键菜单
    document.oncontextmenu = function (eve) {
        return false;
    };
    //自定义右键菜单唤醒和关闭
    document.onmouseup = function (eve) {
        //event.button用来判断鼠标按下的是那个按键。
        //在非IE下,左键0  中键1  右键2
        //IE下,左键1 右键2 中键4
        if(eve.button == 2){
            youjiancaidan.style.left = eve.clientX+'px';
            youjiancaidan.style.top = eve.clientY+'px';
            youjiancaidan.style.display = 'block';
        }else{
            youjiancaidan.style.display = 'none';
        }
    };




    //事件委托写法
    youjiancaidan.onmousedown = function (eve) {
        if(eve.target.innerText == '咱俩去吃麻辣烫吧,6元一碗的那种'){
            setTimeout(function () {
                alert('咱俩去吃麻辣烫吧,6元一碗的那种');
            },10);
        }else if(eve.target.innerText == '是否离开本页面'){
            setTimeout(function () {
                var result = confirm('是否离开本页面?');
                if(result){
                    window.close();
                }
            },10);
        }else if(eve.target.innerText == '将页面中选中的内容去百度搜索'){
            var result = document.getSelection().toString();
            setTimeout(function () {
                if(result){
                    open('http://www.baidu.com/s?wd='+result);
                }else{
                    alert('啥东西都没选中,搜什么搜?');
                }
            },10);
        }else if(eve.target.innerText == '输入内容,然后去百度搜索'){
            setTimeout(function () {
                var result = prompt('请在此输入搜索内容','frank');
                if(result){
                    open('http://www.baidu.com/s?wd='+result);
                }else{
                    alert('啥东西都没输入,搜什么搜?');
                }
            },10);
        }else{
            //do nothing
        }
        youjiancaidan.style.display = 'none';
    };


</script>




</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王十一x

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值