html禁止右鍵方法:1、使用oncontextmenu事件,禁用鼠標(biāo)右鍵的菜單;2、使用onselectstart事件,禁止利用右鍵在網(wǎng)頁(yè)上選取內(nèi)容;3、使用oncopy事件,禁止利用右鍵進(jìn)行復(fù)制。
本教程操作環(huán)境:windows7系統(tǒng)、CSS3&&HTML5版、Dell G3電腦。
oncontextmenu事件禁用右鍵菜單
document.oncontextmenu = function(){ event.returnValue = false; }// 或者直接返回整個(gè)事件 document.oncontextmenu = function(){ return false; }
onselectstart事件禁用網(wǎng)頁(yè)上選取的內(nèi)容
document.onselectstart = function(){ event.returnValue = false; }// 或者直接返回整個(gè)事件 document.onselectstart = function(){ return false; }
oncopy事件禁用復(fù)制
document.oncopy = function(){ event.returnValue = false; }// 或者直接返回整個(gè)事件 document.oncopy = function(){ return false; }
以上三種事件,如果只想單純的禁用鼠標(biāo)右鍵,和復(fù)制粘貼,還可以將它們直接寫(xiě)到HTML中的body上面;
<body oncontextmenu = "return false" ></body> <body onselectstart = "return false" ></body> <body oncopy = "return false" ></body>
推薦學(xué)習(xí):html視頻教程