【问题标题】:javascript: change body height according to fit new contentjavascript:根据适合新内容改变身体高度
【发布时间】:2017-05-29 22:57:02
【问题描述】:

我有一个简单的分层菜单,点击时第一级项目显示他们的孩子。这一切都很好,但扩展菜单项会使其他一些页面元素相互重叠。所以我试图改变元素的高度,但它不起作用。这是我的代码:

function showSubmenu(id) {
        var container = document.getElementById("colonna1_hp"); //container not changing height

        var totalHeight = container.clientHeight;

        var list = document.getElementById("aree_tematiche_hp"); //menu container
        var prevHeight = list.clientHeight;

        //expand menu

        var currHeight = list.clientHeight;
        var diff = currHeight - prevHeight;
        var prova = totalHeight+diff;

        container.style.height = prova + "px";

    }

debuggind 显示所有内容均已正确计算且新高度已正确设置,但我没有看到结果。我做错了什么?

【问题讨论】:

  • 你能给我们展示一个有效的 sn-p/fiddle 吗?
  • 不是真的,这是一个相当大的页面,我无法隔离它的一小部分。所以代码是正确的吗?我应该在其他地方搜索问题吗?

标签: javascript html height


【解决方案1】:

你可以使用:

document.getElementById("elementid").style.height = setHeight + "px"; 

或者使用jquery

$("#elementid").css({ 'height': setHeight + "px" });

其中 setHeight 是高度值,px 是单位。

一个例子:

var setHeight = 1000;

document.getElementById("iddiv").style.height = setHeight + "px"; 
#iddiv {
    background-color: lightgrey;
    width: 300px;
    border: 25px solid green;
    padding: 25px;
    margin: 25px;
}
<div id = "iddiv" >This text is the actual content of the box. We have added a 25px padding, 25px margin and a 25px green border. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>

【讨论】:

  • 我就是这么做的
  • 您的问题可能出在您的html中,如果您需要帮助,请显示您的html。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-06
  • 1970-01-01
  • 2019-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多