水平下拉菜单
你好,我想创建一个像这样的 html 结构:
<ul class="menu">
<li class="top"><a></a></li>
<li><a class="button1"></a></li>
<li class="extend">
<a class="button 1"></a>
<ul>
<li class="last"><a class="button2"></a></li>
</ul>
</li>
<li class="extend">
<a class="button1"></a>
<ul>
<li><a class="button2"></a></li>
<li><a class="button3"></a></li>
<li class="last"><a class="button4"></a></li>
</ul>
</li>
<li><a class="button1"></a></li>
<li class="last"><a class="button2"></a></li>
</ul>
使用这段代码:
$(document).ready(function(){
var menuArray = [
{
'name':'main 1',
'className':'main_1'
},{
'name':'main 2',
'group':'group1'
},{
'name':'main 3',
'group':'group1',
},{
'name':'main 4',
'group':'group2'
},{
'name':'main5',
'group':'group2'
},{
'name':'button'
}
];
var createMenu = function(toolArray){
var menu = $('<ul class="menu"><li class="top"><a></a></li></ul>').appendTo('body');
var groupArray = [];
$.each(toolArray, function(index, value){
var group = value.group || '';
var name = value.name || '';
var elementClass = value.className || '';
if(typeof(value.group) === 'undefined'){
console.log('crete button - no group');
var li = $('<li><a class="'+name+'">'+name+'</a></li>');
li.click(value.onClick);
menu.append(li);
}
else{
if(typeof(groupArray[group]) === 'undefined'){//first element in group
console.log('create first element in group '+group);
groupArray[group] = 1;
var li = $('<li class="'+group+'"><a class="'+elementClass+'">'+name+'</a></li>');
menu.append(li);
}
else{
if(groupArray[group] > 1){//third, ... element in group
var li = $('<li><a class="'+elementClass+'">'+name+'</a></li>');
$('li.'+group+' ul', menu).append(li);
}
else{//second element in group
var li_parent = $('li.'+group, menu).addClass('extend');
var ul = $('<ul><li class="'+group+'"><a class="'+elementClass+'">'+name+'</a></li></ul>');
$(ul).append(li_parent);
}
groupArray[value.group] = groupArray[value.group]+1;
}
}
});
};
createMenu(menuArray);
});
问题是,代码只显示这个:
<ul class="menu">
<li class="top"><a></a></li>
<li><a class="main 1">main 1</a></li>
<li><a class="button">button</a></li>
</ul>
知道为什么吗? http://jsfiddle.net/W4Phv/
Hi I want to create a html structure like this:
<ul class="menu">
<li class="top"><a></a></li>
<li><a class="button1"></a></li>
<li class="extend">
<a class="button 1"></a>
<ul>
<li class="last"><a class="button2"></a></li>
</ul>
</li>
<li class="extend">
<a class="button1"></a>
<ul>
<li><a class="button2"></a></li>
<li><a class="button3"></a></li>
<li class="last"><a class="button4"></a></li>
</ul>
</li>
<li><a class="button1"></a></li>
<li class="last"><a class="button2"></a></li>
</ul>
with this code:
$(document).ready(function(){
var menuArray = [
{
'name':'main 1',
'className':'main_1'
},{
'name':'main 2',
'group':'group1'
},{
'name':'main 3',
'group':'group1',
},{
'name':'main 4',
'group':'group2'
},{
'name':'main5',
'group':'group2'
},{
'name':'button'
}
];
var createMenu = function(toolArray){
var menu = $('<ul class="menu"><li class="top"><a></a></li></ul>').appendTo('body');
var groupArray = [];
$.each(toolArray, function(index, value){
var group = value.group || '';
var name = value.name || '';
var elementClass = value.className || '';
if(typeof(value.group) === 'undefined'){
console.log('crete button - no group');
var li = $('<li><a class="'+name+'">'+name+'</a></li>');
li.click(value.onClick);
menu.append(li);
}
else{
if(typeof(groupArray[group]) === 'undefined'){//first element in group
console.log('create first element in group '+group);
groupArray[group] = 1;
var li = $('<li class="'+group+'"><a class="'+elementClass+'">'+name+'</a></li>');
menu.append(li);
}
else{
if(groupArray[group] > 1){//third, ... element in group
var li = $('<li><a class="'+elementClass+'">'+name+'</a></li>');
$('li.'+group+' ul', menu).append(li);
}
else{//second element in group
var li_parent = $('li.'+group, menu).addClass('extend');
var ul = $('<ul><li class="'+group+'"><a class="'+elementClass+'">'+name+'</a></li></ul>');
$(ul).append(li_parent);
}
groupArray[value.group] = groupArray[value.group]+1;
}
}
});
};
createMenu(menuArray);
});
The problem is, that the code only shows this:
<ul class="menu">
<li class="top"><a></a></li>
<li><a class="main 1">main 1</a></li>
<li><a class="button">button</a></li>
</ul>
Any idea why?
http://jsfiddle.net/W4Phv/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来你已经在这一行中交换了变量:
应该是
Looks like you had the variables swapped in this line:
should be