2013-10-31 44 views
-1

我寫了這個元素動態添加的菜單,但是在添加元素之後父元素不會增長。div元素不增長

CSS:

#frstMenu 
{ 
    position:absolute; 
    top:1.5%; 
    right:1%; 
    width:23%; 
    height:70%; 
    background:rgba(0,0,0,0.6); 
    border-radius: 5px; 
    box-shadow: 0 1px 0 rgba(0,0,0,0.2); 
    cursor: pointer; 
    outline: none; 
    padding-top:1%; 
    overflow:hidden; 
    z-index:2; 
} 

.menulist1{ 
    position:absolute; 
    width:100%; 
    height:15$; 
    top:0%; 
    right:0%; 
    dispaly:none; 
    text-decoration: none; 
    color: #333; 
    clear:both; 
    border-bottom: 1px solid #e6e8ea; 
    z-index:2; 
} 

#menulist 
{ 
    position:absolute; 
    top: 100%; 
    right: 1%; 
    width:23%; 
    height:500%; 
    background: #fff; 
    border-radius: 0 0 5px 5px; 
    border: 1px solid rgba(0,0,0,0.2); 
    border-top: none; 
    border-bottom: none; 
    list-style: none; 
    z-index:1; 
} 

HTML:

> <div id="firstMenuList"> 
>   <div id="frstMenu">choose</div> 
>   <div id="menulist" class="menuList"></div> 
>   <div id="frstList1" class="menuList"></div> <- The child divs are similar this </div> 

的Javascript:

function ccc(prnt , id , cls, r) { 


var ar=new Array("hi","there","hello","world","adsad","asdsad","dsfsdfs","fdsfsdf","sfdsfsdf","soiqeqjek"); 

var parent=document.getElementById(prnt); 

for (var i=0;i<ar.length;i++) 
    { 
    var node=document.createElement("div"); 

    node.setAttribute("id",id+""+i); 
    node.setAttribute("class",cls); 
    node.setAttribute("onclick","select('"+id+""+i+"')"); 
    node.style.top=(((i)*15)+2)+"%"; 
    node.style.right=r+"%"; 

    node.innerHTML=ar[i]; 
    parent.appendChild(node); 
    } 
    parent.style.display="none"; 
} 

我如何調用福nction:

ccc("menulist","frstMenu","menulist1","0"); 

圖片:
http://i.stack.imgur.com/BpMPX.jpg

+2

沒有太多注意到你的代碼,但是你正在使用'position:absolute;'所以你期望什麼? –

+1

您的CSS中有一些拼寫錯誤... 15 $,顯示:無。 – JAL

+0

謝謝,我沒有看到任何相關的例子:( –

回答

0

隨着position: absolute必須增加你的菜單裏面循環(對於) - 只需添加高度,同時要添加的每個元素。

代碼添加高度不使用jQuery應該在你的情況是這樣的:

parent.style.width = parent.clientHeight + 20; 

但其當然的例子,你必須改變「20」的價值,您自己的(計算值)。