禁止文本被选中

如何禁止文本被选中,下面介绍一下方法。

1、在CSS中

body {
    -moz-user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
    -khtml-user-select: none;
    user-select: none;
} 

2、从js的层面

if(document.all){
   document.onselectstart= function(){return false;}; //for ie
}else{
   document.onmousedown= function(){return false;};
   document.onmouseup= function(){return true;};
}

以上方法兼容IE。

原文地址:https://www.cnblogs.com/mengshou/p/11447754.html