css中让文字不能被选中的方法是什么?
css中可以使用user-select属性设置文字不能选中,为文本所在元素设置user-select:none样式即可使元素内文本不可选中。
css让文字不能选中的代码如下:
-webkit-user-select:none;
-moz-user-select:none;
-ms-user-select:none;
user-select:none;user-select属性说明:
语法:
user-select:none |text| all | element默认值:text
适用于:除替换元素外的所有元素
继承性:无
动画性:否
计算值:指定值
取值:
none:文本不能被选择
text:可以选择文本
all:当所有内容作为一个整体时可以被选择。如果双击或者在上下文上点击子元素,那么被选择的部分将是以该子元素向上回溯的最高祖先元素。
element:可以选择文本,但选择范围受元素边界的约束
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<style>
.test{
padding:10px;
-webkit-user-select:none;
-moz-user-select:none;
-o-user-select:none;
user-select:none;
background:#eee;}
</style>
</head>
<body>
<div onselectstart="return false;" unselectable="on">
选择我试试,你会发现怎么也选择不到我,哈哈哈哈</div>
</body>
</html>本文内容仅供个人学习/研究/参考使用,不构成任何决策建议或专业指导。分享/转载时请标明原文来源,同时请勿将内容用于商业售卖、虚假宣传等非学习用途哦~感谢您的理解与支持!