【问题标题】:How to add height to wrapper with jQuery?如何使用 jQuery 为包装器添加高度?
【发布时间】:2019-10-27 00:59:15
【问题描述】:

我需要使用 jQuery 为每个页面动态添加 css 高度。

到目前为止,我得到了以下代码,但它并没有给#wrapper 增加高度。

function getPageSizeWithScroll(){
if (window.innerHeight && window.scrollMaxY) {// Firefox
    yWithScroll = window.innerHeight + window.scrollMaxY;
    xWithScroll = window.innerWidth + window.scrollMaxX;
} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    yWithScroll = document.body.scrollHeight;
    xWithScroll = document.body.scrollWidth;
} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
    yWithScroll = document.body.offsetHeight;
    xWithScroll = document.body.offsetWidth;
}
arrayPageSizeWithScroll = yWithScroll;
//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
return arrayPageSizeWithScroll;
}
var newheight = getPageSizeWithScroll() + 30;

$('#wrapper').css({'height':'newheight'});

【问题讨论】:

    标签: jquery css height


    【解决方案1】:

    试试

    $('#wrapper').css("height", newheight);
    

    【讨论】:

      【解决方案2】:

      您将高度设置为字符串“newheight”,而不是 newheight 的值。只需去掉引号:

      $('#wrapper').css({'height':newheight});
      

      【讨论】:

        【解决方案3】:

        $('#wrapper').css({height:newheight+'px');
        

        【讨论】:

        • 或 +'em' 或任何其他,如果您选择不使用像素(当然逻辑会从 30 值更改),这个花絮是 +1
        【解决方案4】:

        这应该可行:

        $('#wrapper').css({'height':newheight});
        

        【讨论】:

          【解决方案5】:

          您正在为您的身高分配一个字符串值,您只需要删除引号并简单地放置一个包含值的变量。喜欢

          var newheight = getPageSizeWithScroll() + 30;
          
          $('#wrapper').css('height':newheight);
          

          【讨论】:

            【解决方案6】:

            或者

            $('#wrapper').height(newheight);
            

            做同一件事的方法有很多。 height method

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-05-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多