jQuery $(“body”).height() returns undefined

前端 未结 2 1192
孤独总比滥情好
孤独总比滥情好 2021-01-18 23:29

I have a call like this:

$(\"#ContextMenuModal\").height($(\"body\").height());

However $(\"body\").height() returns undefined

相关标签:
2条回答
  • 2021-01-18 23:48

    Simply use

    $(document).height() // - $('body').offset().top
    

    and / or

    $(window).height()
    

    instead $('body').height()

    To expand a bit,

    $(window).height();   // returns height of browser viewport
    $(document).height(); // returns height of HTML document
    

    As bažmegakapa points out, there is a slight difference, albeit a few pixels. The true height of the body can be calculated by subtracting the body offset from the document height (like I mentioned above):

    $(document).height() - $('body').offset().top
    
    0 讨论(0)
  • 2021-01-19 00:04

    Use this-

     $("body").css("height")
    

    well i will say jquery is not needed-

    this.style.height;//it will not work if you add it as inline js.
    
    0 讨论(0)
提交回复
热议问题