javascript:根据合适的新内容改变身高

时间:2017-05-29 14:27:53

标签: javascript html height

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

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显示正确计算了所有内容并正确设置了新高度,但我看不到结果。我做错了什么?

1 个答案:

答案 0 :(得分:0)

你可以使用:

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>