0

コードはとてもシンプルです。

HTML:

<div class="simpleDiv" id="Child1" onmouseover="expandDiv(this);"></div>

CSS:

.simpleDiv {
    background-color: red; 
    width: 100px; 
    height: 50px; 
    margin: 5px; 
    margin-left: 50px; 
    opacity: 1;
}   

JavaScript:

function expandDiv(object){
    alert(document.getElementById(object.id).style.height);
} 

このように div の高さを警告できないのはなぜですか? 関数 hasAttribute を使用して ID またはクラスを警告すると、正常に機能しますが、要素の css プロパティを警告することはできません。

どんな助けでも大歓迎です!

4

4 に答える 4

6

だけではないのはなぜalert(object.style.height)ですか?

elem.style.propertyとにかく、それが機能しない理由は、インラインstyle="..."属性でしか機能しないためです。すべてのスタイルを考慮するには、次のものが必要です。

alert(window.getComputedStyle(object).height)

古いバージョンの IE はこれをサポートしていませんが、非常に簡単な shim です。

window.getComputedStyle = window.getComputedStyle || function(e) {return e.currentStyle;};
于 2013-03-20T14:17:05.377 に答える
2
function expandDiv(object){



    alert(document.getElementById(object.id).innerHeight);

}
于 2013-03-20T14:20:02.733 に答える
1

使用してみてください:

alert(document.getElementById(object.id).offsetHeight);

ここに説明があります: MDNのoffsetHeight

于 2013-03-20T14:18:16.790 に答える
1

JQuery を使用します。

 alert($('#Child1').css("height");

または、属性を変更するには、次を使用します。

$('#Child1').css("height", value )

JQuery が不要な場合は無視してください。

于 2013-03-20T14:22:46.597 に答える