zoukankan      html  css  js  c++  java
  • 使用javascript实现html文字不可选

    如何使用js让html该文本是不可选定它?首先想到的是用css选择实现,如下面:

    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;

    可是这样并不能兼容旧的浏览器,所下面本文将讨论怎样使用js来实现。并兼容全部浏览器。

    首先想到的是:

    <!doctype html>
    <html lang="en">
        <head>
            <title>SO question 2310734</title>
            <script>
                window.onload = function() {
                    var labels = document.getElementsByTagName('label');
                    for (var i = 0; i < labels.length; i++) {
                        disableSelection(labels[i]);
                    }
                };
                function disableSelection(element) {
                    if (typeof element.onselectstart != 'undefined') {
                        element.onselectstart = function() { return false; };
                    } else if (typeof element.style.MozUserSelect != 'undefined') {
                        element.style.MozUserSelect = 'none';
                    } else {
                        element.onmousedown = function() { return false; };
                    }
                }
            </script>
        </head>
        <body>
            <label>Try to select this</label>
        </body>
    </html>


    这样就能够完毕html文本不可选了,假设你在使用jQuery也能够扩展JQuery插件的方式来实现:


    <!doctype html>
    <html lang="en">
        <head>
            <title>SO question 2310734 with jQuery</title>
            <script src="http://code.jquery.com/jquery-latest.min.js"></script>
            <script>
                $.fn.extend({ 
                    disableSelection: function() { 
                        this.each(function() { 
                            if (typeof this.onselectstart != 'undefined') {
                                this.onselectstart = function() { return false; };
                            } else if (typeof this.style.MozUserSelect != 'undefined') {
                                this.style.MozUserSelect = 'none';
                            } else {
                                this.onmousedown = function() { return false; };
                            }
                        }); 
                    } 
                });
    
                $(document).ready(function() {
                    $('label').disableSelection();            
                });
            </script>
        </head>
        <body>
            <label>Try to select this</label>
        </body>
    </html>

    或者:

    (function ($) {
    $.fn.disableSelection = function () {
        return this.each(function () {
            if (typeof this.onselectstart != 'undefined') {
                this.onselectstart = function() { return false; };
            } else if (typeof this.style.MozUserSelect != 'undefined') {
                this.style.MozUserSelect = 'none';
            } else {
                this.onmousedown = function() { return false; };
            }
        });
    };
    })(jQuery);
    
    $(document).ready(function() {
        $('label').disableSelection();
    
        // Or to make everything unselectable
        $('*').disableSelection();
    });

    行,这可以与所有的浏览器基本相容。



    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    JavaScript 深入之从原型到原型链(转载)
    Javascript 异步加载详解(转载)
    JavaScript 运行机制(转载)
    js学习总结----数据类型检测的四种方式(转载)
    GitHub常用指令
    项目部署到linux云服务器最简单的方式
    把MongoDB写成服务
    浏览器中的事件循环
    使用Nodejs计算文件夹中所有文件的大小
    前端web模块化开发之ESModules
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4662492.html
Copyright © 2011-2022 走看看