0

jquery で div の高さを auto に設定し、その高さを再度計算しようとすると、結果として 0 が返されます。これはなぜですか?

$('#test').css('height','auto')

$('#test').height(); // 0

代わりにその高さを計算するにはどうすればよいですか?

編集

これは私が実行しているJavaScriptコードです:

function visitFix() {
    $('.visit').find('.profileDetail').each(function () {
        console.log($(this).height()); //24
        $(this).css('height', 'auto');
       console.log($(this).height()); // 0
    });
}

DOM ツリーは次のようになります。

<td class="profileView">
    <div class="profileContent">Purpose: </div>
    <div class="profileDetail" style="height: auto;">Program Participant Volunteer Rejuvenation Participant General Visit </div>
</td>

出力は 24 で、次に 0 です。

4

3 に答える 3

1

これを試して

http://jsfiddle.net/zS7kd/

.height(), .innerHeight()または必要に応じて使用できますouterHeight()

http://api.jquery.com/height/

ここに画像の説明を入力

.height() -padding、border、margin を除いた要素の高さを返します。

.innerHeight() -要素の高さを返します。パディングは含まれますが、ボーダーとマージンは除外されます。

.outerHeight() -ボーダーを含み、マージンを除く div の高さを返します。

.outerHeight(true) -マージンを含む div の高さを返します。

于 2013-08-02T06:31:01.487 に答える
0

私たちが入ったとき

CSS

{height:auto}

html

<div id="test"></div>

jQuery

 $(document).ready(function(e) {
        alert($("#test").height());
    });

出力は0です

<div id="test">dsdsdsd</div>

 $(document).ready(function(e) {
        alert($("#test").height());
    });

出力は20です

height :auto を設定すると、自動的に高さが計算されます

パディングとマージンで高さを取得したい場合は、次のような関数がさらにあります

参考文献:

.innerHeight() - 要素の高さを返します。パディングは含まれますが、ボーダーとマージンは除外されます。

.outerHeight() - 境界線を含む div の高さを返しますが、マージンは除外します。

于 2013-08-02T06:47:03.150 に答える