Web ページにテキストをラベルとして追加し、選択できないようにしたいと考えています。
つまり、マウスカーソルがテキストの上にあるときに、テキスト選択カーソルにまったく変わらないようにしたいと思います。
私が達成しようとしていることの良い例は、この Web サイトのボタン (質問、タグ、ユーザーなど) です。
Web ページにテキストをラベルとして追加し、選択できないようにしたいと考えています。
つまり、マウスカーソルがテキストの上にあるときに、テキスト選択カーソルにまったく変わらないようにしたいと思います。
私が達成しようとしていることの良い例は、この Web サイトのボタン (質問、タグ、ユーザーなど) です。
プレーンなバニラ 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>
正しい CSS バリエーションをすべて含む回答をここに投稿した人は誰もいなかったので、以下に示します。
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
問題に対する最新の完全な解決策は純粋に 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 は非常にエレガントです。
上記の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();
});