今回はJavaScriptの便利な機能の一つ、getSelectionメソッドを使ったテキスト選択のテクニックについて紹介します。 この方法を使えば、ユーザーがページ上でテキストを選択した際、その選択範囲の文字列を取得し、表示することができます。 初心者の方にもわかりやすいように、一歩ずつ進めていきましょう! まずは基本から:getSelectionとは? JavaScriptのgetSelectionメソッドは、ユーザーがウェブページ上でテキストを選択したとき、その選択範囲の情報を取得するのに使います。この機能は、テキストのコピー&ペースト、ハイライト表示、特定のテキストの検索といった場面で非常に便利です。 CSSでスタイリング まずは、ページに適切なスタイルを適用しましょう。CSSでは、テキストやページの基本的な見た目を整えます。例えば、以下のようなスタイルを適用することができます。