jQuery outer height is zero?

2020-08-15 07:42发布

Why is the height of my search form 0? It's at least 20px high.

jQuery(function($) { // Document ready
    var s_height = $("#search_form").outerHeight(); // Height of searchform
    alert(s_height) // 0

标签: jquery height
2条回答
Rolldiameter
2楼-- · 2020-08-15 08:15

With jQuery you can only get the height of an element if it is visible. So make sure that it is visible at the time that your JS runs, or use one of the several workarounds if you need it to be hidden at the time.

As you mentioned in your comments, the div was hidden and then fading in.

查看更多
你好瞎i
3楼-- · 2020-08-15 08:24

If search_form contains floats, try overflow:hidden; on it. If it contains images, try $(window).load(function(){ /* code */ }).

查看更多
登录 后发表回答