Vue实现当前页面禁止鼠标右键,复制文本内容和F12

只在mounted()里面写下面的代码,在进入这个页面前其他页面是能正常的使用鼠标右键,复制文本内容和F12,但进入当前页后再出来就会影响到其他页面

所以要做到只控制当前页,我们需要在destroyed()钩子中把这些禁止重新打开,这样就能实现该功能了

  mounted() {
    setTimeout(() => {
      if (this.copy_switch == false) {
        this.$nextTick(() => {
          // 禁用右键
          document.oncontextmenu = new Function("event.returnValue=false");
          // 禁用选择
          document.onselectstart = new Function("event.returnValue=false");
          // 禁用F12
          document.onkeydown = () => {
            if (window.event && window.event.keyCode == 123) {
              return false
            }
          }
        });
      } else {
        this.$nextTick(() => {
          // 打开右键
          document.oncontextmenu = new Function("event.returnValue=true");
          // 打开选择
          document.onselectstart = new Function("event.returnValue=true");
          // 打开F12
          document.onkeydown = () => {
            if (window.event && window.event.keyCode == 123) {
              return true
            }
          }
        });
      }
    }, 1000);
  }

重新打开

 destroyed() {
    document.onkeydown = () => {
      if (window.event && window.event.keyCode == 123) {
        return true
      }
    }
    document.onselectstart = new Function("event.returnValue=true");
    document.oncontextmenu = new Function("event.returnValue=true");
  },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值