在html中,可以利用user-select属性来禁止文字选择,只需要给文本元素添加“user-select:none;”样式即可;user-select属性用于设置用户是否能够选中文本,当值为“none”可让文本不能被选择。
本教程操作环境windows7系统、css3&&html5版、dell g3电脑。
html中禁止文字被选中
user-select: none |text| all | element;取值
none:文本不能被选择
text:可以选择文本
all :当所有内容作为一个整体时可以被选择。如果双击或者在上下文上点击子元素,这样的话被选择的部分将是以该子元素向上回溯的最高祖先元素。
element:可以选择文本,但选择范围受元素边界的约束
说明
1. ie6-9不支持该属性,但支持使用标签属性onselectstart=return false;来达到user-select:none的效果;safari和chrome也支持该标签属性;
2. 直到opera12.5仍然不支持该属性,但和ie6-9一样,也支持使用私有的标签属性unselectable=on来达到user-select:none的效果;unselectable 的另一个值是 off;
3. 除chrome和safari外,在其它浏览器中,如果将文本设置为-ms-user-select:none;,则用户将无法在该文本块中开始选择文本。然而,如果用户在页面的其他区域开始选择文本,则用户仍然可以继续选择将文本设置为-ms-user-select:none;的区域文本;
4. 对应的脚本特性为userselect。
html
<!doctype html><html><head> <meta charset=utf-8> <meta http-equiv=x-ua-compatible content=ie=edge> <title>user-select</title></head><style> .test { padding: 10px; -webkit-user-select: none; -moz-user-select: none; -o-user-select: none; user-select: none; background: #eee; }</style><body> <div onselectstart=return false; unselectable=on>禁止选中文本内容</div></body></html>推荐教程《html视频教程》