214

Web ページにテキストをラベルとして追加し、選択できないようにしたいと考えています。

つまり、マウスカーソルがテキストの上にあるときに、テキスト選択カーソルにまったく変わらないようにしたいと思います。

私が達成しようとしていることの良い例は、この Web サイトのボタン (質問、タグ、ユーザーなど) です。

4

4 に答える 4

337

プレーンなバニラ 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;
}
<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>

すでにjQuerydisableSelection()を使用している場合は、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>
于 2010-02-22T12:31:44.517 に答える
173

正しい CSS バリエーションをすべて含む回答をここに投稿した人は誰もいなかったので、以下に示します。

-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
于 2011-06-17T07:09:57.040 に答える
62

問題に対する最新の完全な解決策は純粋に 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 は非常にエレガントです。

于 2011-11-23T16:04:53.430 に答える
12

上記の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();
});
于 2011-02-21T20:40:43.193 に答える