做專案的時候有客戶提出要求,不能用使用者瀏覽他發表的文章時複製他的文章
一種比較簡單的方法,禁止使用者選中頁面的文字和禁止使用者右鍵選單
document.oncontextmenu = new Function("event.returnValue=false");
document.onselectstart = new Function("event.returnValue=false");
如果只是禁止部分割槽域的話,直接在標籤上加上onselectstart=“return false”
<div onselectstart="return false" id="div" >
此區域禁止複製
</div>
或者
<div onselectstart="return false" id="div" >
此區域禁止複製
</div>
<script type="text/javascript" >
$(document).ready(function(){
document.getElementById('div').onselectstart =function(){
return false;
};
});
</script>
在火狐下這種js寫法是無效的
在火狐下有個遮蔽選擇樣式的樣式屬性 -moz-user-select (只支援火狐瀏覽器)
<div style="-moz-user-select:none;" >
禁止複製此區域
</div>
如果要禁止整個頁面
<script type="text/javascript" >
$(document).ready(function(){
$("body").css("-moz-user-select","none");
});
</script>
-moz-user-select主要有三個值 none -moz-all -moz-none
Input表單中文字 | 一般頁面文字 | |
none | 不可選 | 不可選 |
-moz-all | 不可選 | 可選 |
-moz-none | 可選 | 不可選 |