テキストボックス選択時に背景色を変える

サンプルソース

<input type="text" value="テキスト1" onfocus="this.style.backgroundColor='#cccccc'" onblur="this.style.backgroundColor='#ffffff'">
<input type="text" value="テキスト2" onfocus="this.style.backgroundColor='#cc0000'" onblur="this.style.backgroundColor='#ffffff'">

style.backgroundColorで色名や色コードを指定して背景色を設定します。

onfocusはフォーカスしたとき、onblurはフォーカスを失ったときのイベントです。
それぞれのイベントで背景色を変更しています。

実行結果





関連する内容






Copyright (C) JavaScript@Workshop All rights reserved.