有時候,我們為了用戶體驗,需要禁用選擇文本功能。
這需要用到一個CSS屬性:user-select,user-select的文檔點這里
user-select有兩個值:
none:用戶不能選擇文本
text:用戶可以選擇文本
需要注意的是:user-select並不是一個W3C的CSS標准屬性,瀏覽器支持的不完整,需要對每種瀏覽器進行調整
body{ -moz-user-select: none; /*火狐*/ -webkit-user-select: none; /*webkit瀏覽器*/ -ms-user-select: none; /*IE10*/ -khtml-user-select: none; /*早期瀏覽器*/ user-select: none; }
IE6-9還沒發現相關的CSS屬性,只能通過JS來實現
//IE6-9 document.body.onselectstart = document.body.ondrag = function(){ return false; }
這樣我們就可以結合CSS和JS的方法來禁用瀏覽器的文本選擇功能了
有時候,我們為了用戶體驗,需要禁用選擇文本功能。
這需要用到一個CSS屬性:user-select,user-select的文檔點這里
user-select有兩個值:
none:用戶不能選擇文本
text:用戶可以選擇文本
需要注意的是:user-select並不是一個W3C的CSS標准屬性,瀏覽器支持的不完整,需要對每種瀏覽器進行調整
- body{
- -moz-user-select: none; /*火狐*/
- -webkit-user-select: none; /*webkit瀏覽器*/
- -ms-user-select: none; /*IE10*/
- -khtml-user-select: none; /*早期瀏覽器*/
- user-select: none;
- }
IE6-9還沒發現相關的CSS屬性,只能通過JS來實現
- //IE6-9
- document.body.onselectstart = document.body.ondrag = function(){
- return false;
- }
這樣我們就可以結合CSS和JS的方法來禁用瀏覽器的文本選擇功能了