最初,我生成dropdown menu
通過:如何在JavaScript中動態創建下拉菜單列表?
我已經添加了更多關於如何創建下拉菜單的詳細信息。
<div class="well sidebar-nav">
<img src="building.gif" style="display: block;margin-left:auto; margin-right:auto;max-width:100%; max-height:100%;" />
<div class="dropdown" style="padding-left: 20px; padding-top:5px;">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" style="display: block;margin-left:auto; margin-right:auto;">
Select A Building
<span class=" caret">
</span>
</button>
<ul id="myul" class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1"></ul>
</div>
</div>
</div><!--/span2-->
現在,我寫了下面的代碼:
var buildingList=[
{
"BuildingName": "Adler Journalism and Mass Communication Building",
"BuildingNumber": "0456"
},
{
"BuildingName": "Art Building",
"BuildingNumber": "0021"
}
];
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" style="display: block;margin-left:auto; margin-right:auto;">
Select A Building
<span class=" caret">
</span>
</button>
<ul id="myul" class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
</ul>
<script type="text/javascript">
var out = "";
var ul = document.getElementById("myul");
for (i = 0; i < buildingList.length; i++) {
out = ' <li role="presentation"><a role="menuitem" tabindex="-1" href=" ' + buildingList[i].BuildingName + ' "> ' + buildingList[i].BuildingName + ' </a></li>';
var li = document.createElement("li");
var t = document.createTextNode(out);
li.appendChild(t);
ul.appendChild(li);
}
</script>
我想生成動態基於使用javascript
的buildingList
陣列上的dropdown
列表,你可以幫我嗎?
在此先感謝!
的更好的辦法是使用select標籤,並進行硬編碼選擇語法作爲一個字符串,並添加子元素我意思是你的for循環裏面的drodown選項,在html中創建一個div,最後你可以使用innerHTML代碼來創建div – 2015-03-02 04:49:39