【Javascript】スタイル属性(色・線・大きさなど)を参照・変更する方法
【Javascript】
スタイル属性(色・線・大きさなど)を参照・変更する方法
Javascriptでは、CSSのスタイル属性を変更できるので、
「背景色」「画像」「レイアウト」「テキスト」などの変更が簡単にできる。
スタイル属性を変更する書式
「Javascript」で、「Style」を利用してレイアウトを変更するには、
「style」オブジェクトとプロパティを利用して、
スタイル属性の参照と設定を行う。
数値の指定の場合、「px」など単位を指定しないと、
反応してくれない。
スタイル属性を変更するサンプルコード
var 変数名 = window.document.querySelector('#ID名').style.スタイル属性="値";
var 変数名 = window.document.getElementById("ID名").style.スタイル属性="値";
window.document.querySelector('#ID名').style.width="100px";
window.document.querySelector('#ID名').style.backgroundColor="#ffffff";
window.document.getElementById("ID名").style.width="100px";
window.document.getElementById("ID名").style.backgroundColor="#ffffff";
スタイル属性のプロパティ一覧
| 要素スタイルの参照・設定。 |
| background | 背景関連 |
| backgroundAttachment | スクロール時の背景動作 |
| backgroundColor | 背景色 |
| backgroundImage | 背景画像 |
| backgroundPosition | 背景画像位置 |
| backgroundPositionX | 背景画像位置 |
| backgroundPositionY | 背景画像位置 |
| backgroundRepeat | 背景画像の繰り返し |
| border | ボーダー関連 |
| borderColor | ボーダー色 |
| borderStyle | ボーダースタイル |
| borderWidth | ボーダーの太さ |
| clear | テキストの回りこみ解除 |
| clip | 表示範囲 |
| color | テキストの色 |
| cssText | スタイルシート |
| cursor | カーソル |
| display | 表示形式 |
| filter | フィルタ |
| font | フォント関連 |
| fontFamily | フォント |
| fontSize | フォントサイズ |
| fontStyle | フォントスタイル |
| fontVariant | アルファベットの大文字化 |
| fontWeight | フォントの太さ |
| height | 高さ |
| left | 左からの位置 |
| letterSpacing | 文字間スペース |
| lineHeight | 行の高さ |
| listStyle | リストのスタイル |
| listStyleImage | リストのスタイルイメージ |
| listStylePosition | リストのスタイル位置 |
| listStyleType | リストのスタイルタイプ |
| margin | マージン関連 |
| marginBottom | 下マージン |
| marginLeft | 左マージン |
| marginRight | 右マージン |
| marginTop | 上マージン |
| overflow | スクロールバー制御 |
| paddingBottom | 下パディング |
| paddingLeft | 左パディング |
| paddingRight | 右パディング |
| paddingTop | 上パディング |
| pageBreakAfter | 改ページ |
| pageBreakBefore | 改ページ |
| pixelHeight | 高さ |
| pixelLeft | 左からの位置 |
| pixelTop | 上からの位置 |
| pixelWidth | 幅 |
| posHeight | 高さ |
| position | 配置方式 |
| posLeft | 左からの位置 |
| posTop | 上からの位置 |
| posWidth | 幅 |
| styleFloat | 回り込み配置 |
| textAlign | テキストの表示位置 |
| textDecoration | テキスト修飾 |
| textDecorationBlink | テキスト修飾 |
| textDecorationLineThrough | テキスト修飾 |
| textDecorationNone | テキスト修飾 |
| textDecorationOverline | テキスト修飾 |
| textDecorationUnderline | テキスト修飾 |
| textIndent | テキストのインデント |
| textTransform | 単語の先頭の大文字化 |
| top | 上からの位置 |
| verticalAlign | 縦方向のテキスト位置 |
| visibility | 可視・不可視 |
| width | 幅 |
| zIndex | 重なり順序 |
Back