通過css實現頁面文字不能被選中
.cannotselect { -webkit-touch-callout: none; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; }
css介紹 user-select 說明
控制選取能否被選擇。
該特性是非標準的,請盡量不要在生產環境中使用它!
Formal syntax: none | text | all | element
none:元素內的文字及其子元素將不會被選中
text:用戶可以選中文字
all:在一個HTML編輯器中,當雙擊子元素或者上下文時,那么包含該子元素的最頂層元素也會被選中。
element:火狐和IE中有效. Enables selection to start within the element; however, the selection will be contained by the bounds of that element.
瀏覽器兼容性
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 加前綴-webkit- | 加前綴-moz | 10版本以上加前綴-ms | - | 加前綴-webkit |
element | 未實現 | 支持 | 支持 | 未實現 |
-webkit-touch-callout 說明
這個CSS 屬性禁用了默認的callout展示, callout是指當觸摸并按住一個元素的時候出現的提示。
當在iOS上一直按住一個目標元素時,Safari會展示一個關于這個鏈接的callout信息。webkit-touch-callout屬性允許禁用掉這一行為。
-webkit-touch-callout 屬性最早在 iOS 2.0 上實現,后來被添加到WebKit
允許的值
default:此值表示顯示默認的callout
none:此值表示禁用callout
瀏覽器兼容性
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 未實現 | 未實現 | 未實現 | 未實現 | 支持 |
除了css外,我們同樣可以使用js來實現:
document.body.onselectstart = document.body.ondrag =function(){ return false; }