96

ウェブサイトでのテキスト選択を無効にするために JavaScript を使用しています。

コードは次のとおりです。

<script type="text/JavaScript">

function disableselect(e) {
  return false
}

function reEnable() {
  return true
}

document.onselectstart = new Function ("return false")

if (window.sidebar) {
  document.onmousedown = disableselect
  document.onclick = reEnable
}
</script>

同様のスクリプトはここにあります

私のローカルホスト:すべてのブラウザ (Firefox、Chrome、IE、Safari) がうまく動作します。

私のライブ サイト: Firefox 以外はすべて問題ありません。

私の質問は次のとおりです。

  1. ライブ サイトとローカル ホストで Firefox の動作が異なる理由について、誰か提案がありますか。注: Javascript が有効になっています。

  2. たぶん私のスクリプトは単純すぎるので、まったく同じ結果で次のことを試しました

4

6 に答える 6

234

このcssメソッドを使用してください:

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

ここで同じ答えを見つけることができます: CSS を使用してテキスト選択の強調表示を無効にする方法は?

于 2013-05-29T04:52:30.980 に答える
31

ドラッグ機能を提供するスライダー ui コントロールを作成しています。これは、ユーザーがドラッグしているときにコンテンツが選択されないようにする方法です。

function disableSelect(event) {
    event.preventDefault();
}

function startDrag(event) {
    window.addEventListener('mouseup', onDragEnd);
    window.addEventListener('selectstart', disableSelect);
    // ... my other code
}

function onDragEnd() {
    window.removeEventListener('mouseup', onDragEnd);
    window.removeEventListener('selectstart', disableSelect);
    // ... my other code
}

startDragあなたのdomにバインドします:

<button onmousedown="startDrag">...</button>

すべての要素でテキスト選択を静的に無効にする場合は、要素が読み込まれるときにコードを実行します。

window.addEventListener('selectstart', function(e){ e.preventDefault(); });

      
于 2017-02-23T14:54:42.553 に答える
11

JavaScript の場合、次の関数を使用します。

function disableselect(e) {return false}
document.onselectstart = new Function (return false)
document.onmousedown = disableselect

選択を有効にするには、次を使用します。

function reEnable() {return true}

この関数を好きな場所で使用します。

document.onclick = reEnable
于 2014-12-12T00:00:17.663 に答える
5

次のような html ページを取得した場合:

    <本体
    onbeforecopy = "false を返す"
    ondragstart = "false を返す"
    onselectstart = "false を返す"
    oncontextmenu = "false を返す"
    onselect = "document.selection.empty()"
    oncopy = "document.selection.empty()">

すべてのイベントを無効にする簡単な方法があります。

document.write(document.body.innerHTML)

HTML コンテンツを取得し、他のものを失いました。

于 2018-05-03T11:02:52.693 に答える