JS實現(xiàn)禁止鼠標右鍵的功能
遇到網(wǎng)頁上有精美圖片或者精彩文字想保存時,通常大家都是選中目標后按鼠標右鍵,在彈出菜單中選擇“圖片另存為”或“復制”來達到我們的目的。但是,目前有許多網(wǎng)頁都屏蔽了鼠標右鍵,那么用js如何實現(xiàn)禁止鼠標右鍵的功能呢?
1.與禁止鼠標右鍵相關(guān)的JS說明
<script type="text/javascript"> document.oncontextmenu=new Function("event.returnValue=false;"); document.onselectstart=new Function("event.returnValue=false;"); </script>
2.禁止鼠標右鍵火狐失靈
<!DOCTYPE html> <html> <head> <title>禁止鼠標右鍵</title> <meta charset="utf-8"> </head> <body> <div class="poo">這個頁面不能使用鼠標右鍵</div> <!-- 禁止鼠標右鍵 --> <script type="text/javascript"> if (window.Event){ document.captureEvents(Event.MOUSEUP); } function nocontextmenu(){ event.cancelBubble = true event.returnValue = false; return false; } function norightclick(e) { if (window.Event) { if (e.which == 2 || e.which == 3) return false; } else if (event.button == 2 || event.button == 3){ event.cancelBubble = true event.returnValue = false; return false; } } document.oncontextmenu = nocontextmenu; // for IE5+ document.onmousedown = norightclick; // for all others </script> </body> </html>
3.禁止選擇文本
<script type="text/javascript"> var omitformtags=["input", "textarea", "select"]; omitformtagsomitformtags=omitformtags.join("|"); function disableselect(e){ if (omitformtags.indexOf(e.target.tagName.toLowerCase())==-1){ return false; } } function reEnable(){ return true; } if (typeof document.onselectstart!="undefined"){ document.onselectstart=new Function ("return false"); }else{ document.onmousedown=disableselect; document.onmouseup=reEnable; } </script>
4.屏蔽ctrl按鍵
document.onkeydown=function(){ if(event.ctrlKey)return false; }
以上所述是小編給大家介紹的JS實現(xiàn)禁止鼠標右鍵的功能,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的,在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
JavaScript實現(xiàn)跑馬燈抽獎活動實例代碼解析與優(yōu)化(一)
這篇文章主要介紹了JavaScript實現(xiàn)跑馬燈抽獎活動實例代碼解析與優(yōu)化(一)的相關(guān)資料,需要的朋友可以參考下2016-02-02JS實現(xiàn)關(guān)鍵字搜索時的相關(guān)下拉字段效果
關(guān)鍵字搜索時有下拉字段,在使用百度時會遇到,本例講述用js實現(xiàn)類似的效果2014-08-08JavaScript DOM元素常見操作詳解【添加、刪除、修改等】
這篇文章主要介紹了JavaScript DOM元素常見操作,包括針對dom元素的添加、刪除、修改等相關(guān)操作實現(xiàn)技巧與注意事項,需要的朋友可以參考下2018-05-05bootstrap treeview 樹形菜單帶復選框及級聯(lián)選擇功能
這篇文章主要介紹了bootstrap treeview 樹形菜單帶復選框及級聯(lián)選擇功能,代碼超簡單,感興趣的朋友跟隨腳本之家小編一起學習吧2018-06-06