如何使HTML文本无法选择

Ben*_*Ben 171 html css cross-browser highlighting textselection

我想在我的网页上添加文字作为标签并使其无法选择.

换句话说,当鼠标光标在文本上方时,我希望它根本不会变成文本选择光标.

我想要实现的一个很好的例子就是这个网站上的按钮(问题,标签,用户......)

Bal*_*usC 242

你不能用普通的HTML来做到这一点,所以JSF也不能为你做很多事情.

如果你只针对不错的浏览器,那么只需使用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;
}
Run Code Online (Sandbox Code Playgroud)
<label class="unselectable">Unselectable label</label>
Run Code Online (Sandbox Code Playgroud)

如果您还想覆盖旧浏览器,请考虑此JavaScript后备:

<!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>
Run Code Online (Sandbox Code Playgroud)

如果您已经在使用jQuery,那么这是另一个disableSelection()向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>
Run Code Online (Sandbox Code Playgroud)

  • http://caniuse.com/user-select-none (2认同)

Blo*_*sie 167

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

-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
Run Code Online (Sandbox Code Playgroud)

  • 这些在现代浏览器中运行良好,但请注意,像IE7这样的旧版本不支持此功能. (3认同)
  • 在Chrome 18中,这只是一种视觉效果,文字仍然可以选择. (2认同)
  • 仍然可以在 Opera 11 中选择文本 (2认同)
  • @RonRoyston 问题是关于选择不复制。但也许你想要 [this](https://jsfiddle.net/6ppm9jt5/1/)。 (2认同)

Vic*_*eld 52

解决问题的完全现代解决方案纯粹基于CSS,但请注意旧版浏览器不支持它,在这种情况下,您需要回退到其他人提供的解决方案.

所以在纯CSS中:

-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none;
Run Code Online (Sandbox Code Playgroud)

但是,当超过元素的文本时,鼠标光标仍将更改为插入符号,因此您添加到:

cursor: default;
Run Code Online (Sandbox Code Playgroud)

现代CSS非常优雅.

  • 你称之为现代?必须指定相同的东西6次? (34认同)
  • @FlavorScape我使用[less](http://lesscss.org),所以我只需要编写一次这个片段!:) (3认同)

小智 11

我修改了上面发布的jQuery插件,因此它可以在live元素上运行.

(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);
Run Code Online (Sandbox Code Playgroud)

那么你可以这样:

$(document).ready(function() {
    $('label').disableSelection();

    // Or to make everything unselectable
    $('*').disableSelection();
});
Run Code Online (Sandbox Code Playgroud)