我创建了一个菜单。现在我想创建一个动态子菜单。我使用javascript从数据库调用数组。现在如何使用javascript将数组元素与子菜单列表连接起来。
enter code here
// Request For array from database using function gameNames()
// I define array
var gameName = new Array();
// database responses and get array list in this array.
// I dont know how to code for create a array list as a submenu
div id = "bar">
<ul id = "ul1">
<li class = "li1"><a href="#" class = "a1">Games</a>
<ul id = "submenu"><li></li></ul>
</li>
<li class = "li1"><a href="#" class = "a1">Stake</a></li>
<li class = "li1"><a href="#" class = "a1">Max Players</a></li>
</ul>
<img src = "css/images/table_menu.png" id = "tm"/>
<div id = "rmg">Real Money Game</div>
</div>
答案 0 :(得分:0)
这是一个演示。 SubMenu()
函数中的源代码是您要用于从数组中创建列表项的内容。
var gameName=['Game one','Game Two','Game three']
function SubMenu(){
//Set variable for the submenu ul element
var Submenu = document.getElementById('submenu');
//For loop - for each value in the array
for(var i=0; i<gameName.length; i++){
//Create new li/List Item
var Item = document.createElement('li');
//Set innerHTML for this element
Item.innerHTML='<a href="#">'+gameName[i]+'</a>';
//Append new element to the submenu.
Submenu.appendChild(Item);
}
//---Demo use Only
document.getElementsByTagName("button")[0].remove();
//---
}
<button onclick="SubMenu();">Create</button>
<ul>
<li><a href="#">Games</a>
<ul id="submenu"></ul>
</li>
<li><a href="#">Stake</a></li>
<li><a href="#">Max PLayers</a></li>
<ul>
如果您对上述源代码有任何疑问,请在下面留言,我会尽快给您回复。 如果这回答了您的问题,则会通过标记来表示感谢。
我希望这会有所帮助。快乐的编码!