通过CSS规则禁止选中文字的代码实例

黄舟
发布: 2017-04-18 09:44:17
原创
1533 人浏览过

禁止选中文字在某些场景下还是有必要的,比如不想让他人复制自己的文章之类的。这时候我们可以通过使用CSS+JS来解决这一问题。另外,这边要指出的是,user-select目前还不是W3C的正式标准,各个浏览器都是以私有属性的方式提供支持。

语法

Formal syntax: none | text | all | element
登录后复制

代码如下:

(-prefix-)user-select: none; (-prefix-)user-select: text; (-prefix-)user-select: all; (-prefix-)user-select: element;
登录后复制

代码如下:

.row-of-icons { -webkit-user-select: none; /* Chrome all / Safari all */ -moz-user-select: none; /* Firefox all */ -ms-user-select: none; /* IE 10+ */

/* No support for these yet,use at own risk */ -o-user-select: none; user-select: none; }

登录后复制

IE兼容性

目前在IE 10以及IE 10以上版本的浏览器可以使用-ms-user-select这个规则,但在更早版本的IE,我们只能通过javascript实现禁止选中文本:

代码如下:

$(el).attr('unselectable','on') .css({'-moz-user-select':'-moz-none', '-moz-user-select':'none', '-o-user-select':'none', '-khtml-user-select':'none', /* you could also put this in a class */ '-webkit-user-select':'none',/* and add the CSS class here instead */ '-ms-user-select':'none', 'user-select':'none' }).bind('selectstart', function(){ return false; });
登录后复制


以上是通过CSS规则禁止选中文字的代码实例的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
使用jQuery: 在输出中添加数组索引值 我是jQuery和JavaScript的新手。设法使以下代码工作,根据用户从(先前的)下拉选择字段中选择的编号添加新的文本输入字段。