2017-03-26 5 views
0

htmlタグにスタイルシートを使用してfont-size:スタイルを適用しました。私はjavascriptでそれを取得したいと思います。htmlタグのスタイルプロパティを取得

私はdocument.getElementsByTagName("html")[0].styleを実行しようとしましたが、値のないスタイルを取得することになります。

+1

は[ 'window.getComputedStyleが()'ですメソッド](https://developer.mozilla.org/ja/docs/Web/API/Window/getComputedStyle)あなたが探しているものは? – nnnnnn

+0

@nnnnnn私はそれを適用しようとしましたが、失敗しました – Sequential

+0

@nnnnnnもう一度試してみました...ありがとうございます。 ' – Sequential

答えて

1

html要素は、以下を試してみてくださいdocument.documentElementで参照することができます。

var elem = window.getComputedStyle(document.documentElement); 
 
var fontSize = elem.fontSize; 
 
console.log(fontSize);
html{ 
 
    font-size: 20px; 
 
}
<html> 
 
    <head> 
 
    </head> 
 
    <body> 
 
    </body> 
 
</html>

+0

これは、ありがとう! – Sequential

+0

あなたは大歓迎です。 – Mamun

0

次のことを試してください:

let elem = document.getElementsByTagName("html")[0]; 
console.log(window.getComputedStyle(elem, null).getPropertyValue("font-size")); 
関連する問題