如何使HTML文本无法选择

时间:2010-02-22 12:12:23

标签: html css cross-browser highlighting textselection

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

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

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

4 个答案:

答案 0 :(得分:226)

你不能用普通的HTML来做到这一点,所以JSF也不能为你做太多。

如果您只定位decent browsers,那么只需使用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>

如果您还想覆盖旧版浏览器,请考虑使用此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>

如果您已经在使用jQuery,那么这是另一个向jQuery添加新函数disableSelection()的示例,以便您可以在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>

答案 1 :(得分: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;

答案 2 :(得分:51)

对您的问题的完全现代解决方案纯粹基于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;

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

cursor: default;

现代CSS非常优雅。

答案 3 :(得分:9)

我修改了上面发布的jQuery插件,因此它可以用于实时元素。

(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();
});
相关问题