jquery 禁用浏览器右键菜单,局部div不禁用

jquery禁用浏览器右键,局部div不禁用。

操作:在class为a1/a2/tbox的地方点击鼠标右键,要可以出来右键菜单。在tbox1中点击鼠标右键要禁止鼠标右键菜单。

html:

<body>
    <div>
        <div class="tbox1">
            这块内容要禁用浏览器鼠标右键
        </div>
        <div class="tbox">
            <div class="a1">
                在这块div点击鼠标右键
            </div>
            <div class="a2">
                同级div
            </div>
        </div>
    </div>
</body>

jquery:

$(document).contextmenu(function(e) {
    // console.log(this)
    // console.log(e)
    // console.log(e.target)
    if($(e.target).hasClass("tbox")||$(e.target).parents(".tbox").hasClass("tbox")){
        return true;//不禁止
    }else{
        return false;//禁止
    }
})

拓展:

还可以使用ouseDown 和 e.which实现

$(document).mousedown(function(e) {
    //e.which 1为鼠标左键 2为鼠标中键 3为鼠标右键
    if (e.which == 3) {
        console.log('this' + this)
        console.log('e' + e)
        console.log('e.target' + e.target)
        if($(e.target).hasClass("tbox")||$(e.target).parents(".tbox").hasClass("tbox")){
                return true;//不禁止
        }else{
                return false;//禁止
        }
    }
})

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值