CSS:禁止選中文本

CSS特性:user-select: none;

實現(xiàn)效果:禁止選中指定的文本

Sample code:

<!-- HTML部分 -->
<p class="no-selected">this is a pargraph/p> 
//CSS部分
.no-selected {
-webkit-user-select: none; /*Chrome/ Safari/ Opear新版本*/
-moz-user-select: none; /*Foxfire */
-ms-user-select: none; /*Internet Explorer/ Edge*/
-o-user-select: none; /*Opear老版本*/
-khtml-user-select: none; /* Konqueror */
-webkit-touch-callout: none; /* iOS Safari */
user-select: none;  
/* Nonprefixed version, currently not supported by any browser */
}

note: 在 IE < 10 和Opera < 15中我們需要在需要禁止選中的元素上面添加一個屬性unselectable="on",否則可能不會生效.

MDN:user-select特性是非標準的。請謹慎使用

//Formal syntax: none | text | all | element
(-prefix-)user-select: none;
(-prefix-)user-select: text;
(-prefix-)user-select: all;
(-prefix-)user-select: element;

none:元素內(nèi)的文字及其子元素將不會被選中. Selection can contain these elements. Starting with Firefox 21 none behaves like -moz-none, so selection can be re-enabled on sub-elements using -moz-user-select:text.

text:用戶可以選中文字.-moz-noneThe text of the element and sub-elements will appear as if they cannot be selected. Selection can contain these elements. Selection can be re-enabled on sub-elements using -moz-user-select: text. Starting with Firefox 21 none behaves like -moz-none.

all:在一個HTML編輯器中,當雙擊子元素或者上下文時,那么包含該子元素的最頂層元素也會被選中。

element:火狐和IE中有效. Enables selection to start within the element; however, the selection will be contained by the bounds of that element.

Note:user-select is not currently part of any W3C CSS specification(user-select不是現(xiàn)行W3C CSS規(guī)范的一部分). As such, there are minor differences between the browser implementations. Be sure to test your application across browsers.

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容