Can we change the html or body height using jquery?

前端 未结 4 434
走了就别回头了
走了就别回头了 2021-01-13 11:13

I am trying to run this statement: $(\'body\').height(100); but when I check the height again, I find it unchanged!

Is there any way to change the heigh

相关标签:
4条回答
  • 2021-01-13 11:22

    document body is a magical element and does not behave the way other HTML elements do. Your code:

    $('body').height(100);
    

    Is correct and it is same as:

    $('body').css({height: 100});
    $('body').css({height: '100px'});
    $('body').attr({style: 'height: 100px'});
    

    All result in this:

    <body style="height: 100px;">
    

    However, setting the height will not hide the area outside the 100px height invisible. You should wrap the body content inside a div and set its height instead:

    <body>
        <div id="wrapper" style="height: 100px; overflow: hidden;">
            <!-- content -->
        </div>
    </body>
    

    jQuery solution would be:

    $("body").wrapInner($('<div id="height-helper"/>').css({
        height: 100,
        overflow: "hidden"
    }));
    
    0 讨论(0)
  • 2021-01-13 11:35

    You should actually use

    $('body').css('height', '100'); and it will work.

    0 讨论(0)
  • 2021-01-13 11:38

    Try using $('body').addClass('your class name'); or $('body').attr('style', 'height: 100px');

    0 讨论(0)
  • 2021-01-13 11:44

    It should be

    $('body').css('height', '100px');
    

    Or

    $('body').height(100);
    
    0 讨论(0)
提交回复
热议问题