Window getComputedStyle()メソッド

Window 对象参考手册Window オブジェクト


定義と使用方法

getComputedStyle() メソッドは、指定した要素の CSS スタイルを取得するために使用します。

取得したスタイルは、ブラウザで最終的にレンダリングされた要素のスタイルです。

構文

window.getComputedStyle(element, pseudoElement)

パラメータの説明:

  • element: 必須。スタイルを取得する要素。
  • pseudoElement: オプション。擬似クラス要素。擬似クラス要素をクエリしない場合は無視するか、null を渡すことができます。

戻り値

返されるオブジェクトは CSSStyleDeclaration 型のオブジェクトです。

ブラウザサポート

表内の数字は、そのメソッドをサポートする最初のブラウザバージョン番号を示します。

メソッド
getComputedStyle() 11.0 9.0 4.0 5 11.5

例

例

DIV の背景色の値を取得する:

function myFunction(){ var elem = document.getElementById("test"); var theCSSprop = window.getComputedStyle(elem, null).getPropertyValue("background-color"); document.getElementById("demo").innerHTML = theCSSprop; }

試してみる »

Window 对象参考手册Window オブジェクト

その他の拡張