How to make HTML Text unselectable

This question already has an answer here:

  • How to disable text selection highlighting? 37 answers

  • You can't do this with plain vanilla HTML, so JSF can't do much for you here as well.

    If you're targeting decent browsers only, then just make use of CSS3:

    .unselectable {
        -webkit-touch-callout: none;
        -webkit-user-select: none;
        -khtml-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
    }
    
    <label class="unselectable">Unselectable label</label>
    

    If you'd like to cover older browsers as well, then consider this JavaScript fallback:

    <!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>
    

    If you're already using jQuery, then here's another example which adds a new function disableSelection() to jQuery so that you can use it anywhere in your jQuery code:

    <!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>
    

    这里没有人发布了所有正确的CSS变体的答案,所以这里是:

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

    The full modern solution to your problem is purely CSS-based, but note that older browsers won't support it, in which cases you'd need to fallback to solutions such as the others have provided.

    So in pure CSS:

    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    -o-user-select: none;
    user-select: none;
    

    However the mouse cursor will still change to a caret when over the element's text, so you add to that:

    cursor: default;
    

    Modern CSS is pretty elegant.

    链接地址: http://www.djcxy.com/p/7112.html

    上一篇: 如何使某些文本不能用CSS选择

    下一篇: 如何使HTML文本无法选择