CSS设置文字不能被选中及解除限制

今天搜索一些资料,碰到一些网站不能复制文本,

起初以为是js限制问题,使用解除右键的js之后还是不行,

【资料:】

JS禁止选中文本方法
复制代码
if (typeof(element.onselectstart) != "undefined") {        
    // IE下禁止元素被选取        
    element.onselectstart = new Function("return false");        
} else {
    // firefox下禁止元素被选取的变通办法        
    element.onmousedown = new Function("return false");        
    element.onmouseup = new Function("return true");        
} 
复制代码
 

IE下有onselectstart这个方法,通过设置这个方法可以禁止元素文本被选取。而firefox下没有这个方法,但可以通过css或一种变通的办法解决:

使用CSS:

div {
      -moz-user-select:none;
      -webkit-user-select:none;
      user-select:none;    
}
另外一种方法是: 

ie:document.selection.empty()
 ff:window.getSelection().removeAllRanges()
兼容的写法:

window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty(); 

这种方法不但不影响拖放对象的选择效果,还能对整个文档进行清除.


来源:https://www.cnblogs.com/pigtail/archive/2012/09/11/2680462.html

 

搜索相关js:

不是这里控制,

 

搜索css:

右键在source panel打开,

.single-article {/*不允许选中文本*/-webkit-user-select:none;-moz-user-select:none;-o-user-select:none;-ms-user-select:none;user-select:none;}

.single-article pre {color:#dcdcdc;background:scroll #202020;border:1px dashed #ddd;display:block;/*代码块允许选中文本*/user-select:text;-webkit-user-select:text;-moz-user-select:text;-o-user-select:text;-ms-user-select:text;}

删除相关代码Ctrl+S保存即可解除

又可以愉快的复制文本了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章