JSのこのスニペットの問題は誰にでも分かりますか?私はコンテナのmargin-topの値をm変数に代入しようとしています。CSS要素の値を取得する
function test()
{
var e = document.getElementById('container');
var m = e.style.getPropertyValue("marginTop");
alert (m);
}
編集:あなたの変数のスコープを持つように注意して、また
function getStyle(elment, style) {
var x = document.getElementById(element);
if (x.currentStyle) {
var y = x.currentStyle[style];
} else if (window.getComputedStyle) {
var y = document.defaultView.getComputedStyle(x, null).getPropertyValue(style);
}
return y;
}
function test() {
var m = getStyle('container', 'margin-top');
alert(m);
}
:アラートがこれを試してみてくださいメートル
は、明示的に定義された値はありますか? – Christophe
この警告( 'hi')はどのように質問に関連していますか? – gdoron
申し訳ありませんが、私はそれを意味しました:アラート(m); – james