通過(guò)css實(shí)現(xiàn)頁(yè)面文字不能被選中
.cannotselect { -webkit-touch-callout: none; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; }
css介紹 user-select 說(shuō)明
控制選取能否被選擇。
該特性是非標(biāo)準(zhǔn)的,請(qǐng)盡量不要在生產(chǎn)環(huán)境中使用它!
Formal syntax: none | text | all | element
none:元素內(nèi)的文字及其子元素將不會(huì)被選中
text:用戶(hù)可以選中文字
all:在一個(gè)HTML編輯器中,當(dāng)雙擊子元素或者上下文時(shí),那么包含該子元素的最頂層元素也會(huì)被選中。
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 | 未實(shí)現(xiàn) | 支持 | 支持 | 未實(shí)現(xiàn) |
-webkit-touch-callout 說(shuō)明
這個(gè)CSS 屬性禁用了默認(rèn)的callout展示, callout是指當(dāng)觸摸并按住一個(gè)元素的時(shí)候出現(xiàn)的提示。
當(dāng)在iOS上一直按住一個(gè)目標(biāo)元素時(shí),Safari會(huì)展示一個(gè)關(guān)于這個(gè)鏈接的callout信息。webkit-touch-callout屬性允許禁用掉這一行為。
-webkit-touch-callout 屬性最早在 iOS 2.0 上實(shí)現(xiàn),后來(lái)被添加到WebKit
允許的值
default:此值表示顯示默認(rèn)的callout
none:此值表示禁用callout
瀏覽器兼容性
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 未實(shí)現(xiàn) | 未實(shí)現(xiàn) | 未實(shí)現(xiàn) | 未實(shí)現(xiàn) | 支持 |
除了css外,我們同樣可以使用js來(lái)實(shí)現(xiàn):
document.body.onselectstart = document.body.ondrag =function(){ return false; }