JS註冊組|鎖右鍵、複製、選取文字


Posted by itiswonderfall on 2021-12-30

前言

前陣子因為工作需求,要幫客戶網站鎖右鍵,看到工程師貼上這段 code 所以上網查了一下,雖然還是有些 bug ,但現階段來說沒什麼大礙,看到網友 2013 年說「 真訝異現在還有人在玩鎖右鍵… 」,然後現在已經 2021 快邁入 2022 年我們一樣還在做這件事哈哈哈。

圖片來源

 


 

禁止選取文字

body {
    -webkit-touch-callout: none;  
    -webkit-user-select: none;  
    -khtml-user-select: none;  
    -moz-user-select: none;  
    -ms-user-select: none;  
    user-select: none;  
}

 

鎖右鍵、禁止使用 Ctrl 键、禁止使用 Shift 键、禁止使用 Alt 键

function iEsc(){ return false; }
function iRec(){ return true; }
function DisableKeys() {
    if(event.ctrlKey || event.shiftKey || event.altKey)  {
    window.event.returnValue=false;
    iEsc();}
}

document.ondragstart=iEsc;
document.onkeydown=DisableKeys;
document.oncontextmenu=iEsc;

if (typeof document.onselectstart !="undefined") document.onselectstart=iEsc;
else
{
    document.onmousedown=iEsc;
    document.onmouseup=iRec;
}

function DisableRightClick(e)
{
    if (window.Event){ if (e.which == 2 || e.which == 3) iEsc();}
    else
        if (event.button == 2 || event.button == 3)
        {
            event.cancelBubble = true
            event.returnValue = false;
            iEsc();
        }
}

 


 

資料來源

  1. javascript 實現禁止右鍵,複製,選取文字 (相容firefox,IE,chrome等主流瀏覽器)...
  2. javascript 禁止右鍵相關疑問

#js #JS註冊組 #javascript #css #user-select #鎖右鍵







Related Posts

[css] css 界的人生重來槍 - revert

[css] css 界的人生重來槍 - revert

Command Line 初學

Command Line 初學

[ 前端工具 ] - jQuery

[ 前端工具 ] - jQuery


Comments