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

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



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

  • 相关阅读:
    jQueryEasyUi行编辑打造增删改查
    css样式DEMO
    jqueryEasyui常用代码
    Jquery easyui tree 一些常见操作
    EasyUI项目中的自定义JS
    easyui里弹窗的两种表现形式
    EasyUI扩展方法
    JS-easyui 扩展easyui.datagrid,添加数据loading遮罩效果代码
    Being a Hero (hdu 3251 最小割 好题)
    AWS携手上海嘉定政府推出首个联合孵化器 为创业公司拓展AWS云服务可用资源
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4662492.html
Copyright © 2011-2022 走看看