1. 程式人生 > >HTML,JS禁止滑鼠右鍵、禁止全選、複製、貼上的方法

HTML,JS禁止滑鼠右鍵、禁止全選、複製、貼上的方法

禁止滑鼠右鍵、禁止全選、複製、貼上;

oncontextmenu事件禁用右鍵選單;  js程式碼:

document.oncontextmenu = function(){
    event.returnValue = false;
}
// 或者直接返回整個事件
document.oncontextmenu = function(){
    return false;
}

onselectstart事件禁用網頁上選取的內容;  js程式碼:

document.onselectstart = function(){
    event.returnValue = false;
}
// 或者直接返回整個事件
document.onselectstart = function(){
    return false;
}

oncopy事件禁用複製;  js程式碼:

document.oncopy = function(){
    event.returnValue = false;
}
// 或者直接返回整個事件
document.oncopy = function(){
    return false;
}

以上三種事件,如果只想單純的禁用滑鼠右鍵,和複製貼上,還可以將它們直接寫到HTML中的body上面;

<body oncontextmenu = "return false" ></body>

<body onselectstart = "return false" ></body>

<body oncopy = "return false" ></body>

禁用滑鼠事件

document.onmousedown = function(e){
    if ( e.which == 2 ){// 滑鼠滾輪的按下,滾動不觸發
        return false;
    }
    if( e.which==3 ){// 滑鼠右鍵
        return false;
    }
}

禁用鍵盤中的ctrl、alt、shift

document.onkeydown = function(){
    if( event.ctrlKey ){
        return false;
    }
    if ( event.altKey ){
        return false;
    }
    if ( event.shiftKey ){
        return false;
    }
}

關鍵就在  

  oncontextmenu='return false'   ondragstart='return false'    onselectstart ='return false'    onselect='document.selection.empty()'    oncopy='document.selection.empty()'    onbeforecopy='return false'    onmouseup='document.selection.empty()'

一個更簡單的方法就是在<body>中加入如下的程式碼,這樣滑鼠的左右鍵都失效了. 

topmargin="0" 
oncontextmenu="return false" ondragstart="return false" onselectstart 
="return false" onselect="document.selection.empty()" 
oncopy="document.selection.empty()" onbeforecopy="return false" 
onmouseup="document.selection.empty()" 

1.禁止網頁另存為:在<body>後面加入以下程式碼: 
<noscript> 
<iframe src="*.htm"></iframe> 
</noscript> 


2.禁止網頁內容複製.貼上:在<body>中加入以下程式碼: 
<body
 onmousemove=/HideMenu()/ oncontextmenu="return false" 
ondragstart="return false" onselectstart ="return false" 
onselect="document.selection.empty()" 
oncopy="document.selection.empty()" onbeforecopy="return false" 
onmouseup="document.selection.empty()">