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();
    });

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



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

  • 相关阅读:
    快速幂模板
    部分有关素数的题
    POJ 3624 Charm Bracelet (01背包)
    51Nod 1085 背包问题 (01背包)
    POJ 1789 Truck History (Kruskal 最小生成树)
    HDU 1996 汉诺塔VI
    HDU 2511 汉诺塔X
    HDU 2175 汉诺塔IX (递推)
    HDU 2077 汉诺塔IV (递推)
    HDU 2064 汉诺塔III (递推)
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4662492.html
Copyright © 2011-2022 走看看