javascript實現禁止復制網頁內容
更新時間:2014年12月16日 10:36:07 投稿:hebedich
這篇文章主要介紹了javascript實現禁止復制網頁內容,需要的朋友可以參考下
做個筆記
復制代碼 代碼如下:
// 禁用右鍵菜單、復制、選擇
$(document).bind("contextmenu copy selectstart", function() {
return false;
});
// 禁用Ctrl+C和Ctrl+V(所有瀏覽器均支持)
$(document).keydown(function(e) {
if(e.ctrlKey && (e.keyCode == 65 || e.keyCode == 67)) {
return false;
}
});
// 設置CSS禁止選擇(如果寫了下面的CSS則不需要這一段代碼,新版瀏覽器支持)
$(function() {
$("body").css({
"-moz-user-select":"none",
"-webkit-user-select":"none",
"-ms-user-select":"none",
"-khtml-user-select":"none",
"-o-user-select":"none",
"user-select":"none"
});
});
防止禁用JavaScript后失效,可以寫在CSS中(新版瀏覽器支持,并逐漸成為標準):
復制代碼 代碼如下:
body {
-moz-user-select:none; /* Firefox私有屬性 */
-webkit-user-select:none; /* WebKit內核私有屬性 */
-ms-user-select:none; /* IE私有屬性(IE10及以后) */
-khtml-user-select:none; /* KHTML內核私有屬性 */
-o-user-select:none; /* Opera私有屬性 */
user-select:none; /* CSS3屬性 */
}
代碼很簡單,實現的功能卻很實用,不過要提示的是,在這個自由的互聯網上其實做禁止復制不是件很值得推廣的事,大家依情況實用吧。
相關文章
開啟Javascript中apply、call、bind的用法之旅模式
在Javascript中,Function是一種對象。Function對象中的this指向決定于函數被調用的方式,使用apply,call 與 bind 均可以改變函數對象中this的指向。2015-10-10