JavaScript でテキスト選択をクリアする 質問する

JavaScript でテキスト選択をクリアする 質問する

答えが見つからない簡単な質問:

JavaScript(またはjQuery)を使って選択解除テキストは選択されたウェブページで?

たとえば、ユーザーがクリックしてドラッグし、テキストの一部をハイライト表示する場合、この選択をクリアする関数 deselectAll() が必要です。どのように記述すればよいでしょうか?

助けてくれてありがとう。

ベストアンサー1

if (window.getSelection) {
  if (window.getSelection().empty) {  // Chrome
    window.getSelection().empty();
  } else if (window.getSelection().removeAllRanges) {  // Firefox
    window.getSelection().removeAllRanges();
  }
} else if (document.selection) {  // IE?
  document.selection.empty();
}

Y氏に感謝。

おすすめ記事