onselectイベント
例
一部のテキストが選択されたら、JavaScriptを実行します。
<input type="text" onselect="myFunction()">
以下の「自分で試してみてください」の例をもっと見てください。
定義と使用法
onselectイベントは、要素でテキストが選択された後に発生します。
onselectイベントは、主に<input type = "text">または<textarea>要素で使用されます。
ブラウザのサポート
Event | |||||
---|---|---|---|---|---|
onselect | Yes | Yes | Yes | Yes | Yes |
構文
HTMLの場合:
<element onselect="myScript">
JavaScriptの場合:
object.onselect = function(){myScript};
JavaScriptでは、addEventListener()メソッドを使用します。
object.addEventListener("select", myScript);
注: addEventListener ()メソッドは、Internet Explorer8以前のバージョンではサポートされていません。
技術的な詳細
泡: | 番号 |
---|---|
キャンセル可能: | 番号 |
イベントタイプ: | ユーザーインターフェイスから生成された場合はUiEvent 、それ以外の場合はEvent |
サポートされているHTMLタグ: | <input type = "file">、<input type = "password">、<input type = "text">、および<textarea> |
DOMバージョン: | レベル2のイベント |
その他の例
例
HTML DOM入力テキストオブジェクトのselect()メソッドを使用して、テキストフィールドのコンテンツを選択します。これが発生すると、onselectイベントが発生し、アラート機能がトリガーされます。
// Select the contents of a text field
function mySelectFunction() {
document.getElementById("myText").select();
}
// Alert some text when the text in the text field has been selected
function myAlertFunction() {
alert("You selected some text!");
}