jQuery实现有选择性的禁止文本选中代码示例
使用css可以实现指定元素的中的文本禁止选中效果,但是纯CSS方式设置不够灵活,如果能够封装成一个函数,并且能够方便的指定哪些元素中的文本不可以被选中。
代码实例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>jQuery实现有选择性的禁止文本选中-搞代码gaodaima.com</title> <style type="text/css"> html,body{height:100%} #thediv{ width:150px; height:50px; background:#CCC; } </style> <script type="text/javascript" src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ (function($){ $.fn.disableSelection=function(){ return this.attr('unselectable','on') .css({'-moz-user-select':'-moz-none', '-moz-user-select':'none', '-o-user-select':'none', '-khtml-user-select':'none', '-webkit-user-select':'none', '-ms-user-select':'none', 'user-select':'none'}) .bind('selectstart', false); }; })(jQuery); $(':not(input,select,textarea)').disableSelection(); }) </script> </head> <body> <div id="thediv">来搞代码啊</div> <textarea></textarea> </body> </html> |
本文案例为大家展示了《jQuery实现有选择性的禁止文本选中代码示例》的代码,若要看代码效果请大家点击“运行代码”按钮。
原创文章,转载请注明: 转载自搞代码
本文链接地址: jQuery实现有选择性的禁止文本选中代码示例

微信 赏一包辣条吧~

支付宝 赏一听可乐吧~
发表评论