获取一个height设置为auto的元素的实际高度

jq

$("#box")[0].offsetHeight

js

document.getElementById("box").offsetHeight

这两个方法是没问题的,但我在项目中遇到了问题。
我要获取隐藏元素iframe中,一个height为auto的元素高度。再将此高度赋给iframe,让高度适应。卸载iframe的load回调函数中,但是获取的高度始终为0。 但是当iframe显示后,再获取便可以获取到。
我的解决办法如下,如有更好的方案,一起交流。

if($("#show-box-research").is(':visible')){
            var height = $("#show-box-research iframe").contents().find("#subjec")[0].offsetHeight
            if(height>800){
                $("#show-box-research iframe").css("height",height+"px");
            }
        }

你可能感兴趣的:(js小技巧,jq)