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

禁止选中文字在某些场景下还是有必要的,比如不想让他人复制自己的文章之类的。这时候我们可以通过使用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规则禁止选中文字的代码实例的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1608544.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
CSS3制作动画下拉菜单效果代码
上一篇 2025年12月23日 21:56:45
纯CSS3实现不错的表单验证效果的代码分享(图)
下一篇 2025年12月23日 21:56:57

相关推荐

发表回复

登录后才能评论
关注微信