这个,其实比较简单
数据上来说,相当于是一个多维数组
比如,系 :
计算机,中文,英语
然后是专业:
计算机 -> 软件工程,xxx专业 ...
中文 -> 汉语言文学,xxx专业 ...
数据库在设计上,你可以将他们都放在一个表里面,可以有这些列
id 唯一id
name 名称,可以是专业名称,也可以是系名称
parent 指定如果是专业,是数据那个系,这里可以存成系的id,
如果是系,那么这里直接填0,表示就可以
这样的话,一个demo数据
id name parent
1 计算机系 0
2 软件工程 1
这样,上边,后台的数据就这个节奏就可以满足,如果专业还有细分,那这个结构也可以满足
前台显示的时候,这里分成俩个select来显示就可以
第一个select用来显示系,查询parent为0的数据,显示到select里面就可以
第二个select用来显示系的专业,查询的时候,将第一个select的系id拿到,直接数据库里面查询parent为第一个select选择系的id的数据
demo
选择系:<select id="department" onchange="change(this)"></select>
选择专业:
<select id="professional"></select> //用js读取数据,然后生成代码
//根据parentid,读取数据,然后返回
function getData(parentId , callback){
$.ajax({
url : 'test.do',//后台数据地址
data : {parentId : parentId},//请求parentid的数据
success : function(result){
alert(result)
callback(result)
}
})
}
function renderOptions(arr , el){//生成select 的 option
el.empty()
$.each(data , function(index , item){
$('<option/>').attr({
value : item.id
}).html(item.name).appendTo(el)
})
}
var department = $('#department')//系的select
getData(0 , function(data){//请求系的数据
department.empty()
renderOptions(data , department)//这里生成系的select
})
var professional = $('#professional')//专业的select
function change(el){//当系变化的时候
var parentId = $(el).val()//这里得到选择的系的id
getData(parentId , function(data){
professional.empty()
renderOptions(data , professional )//这里生成专业的select
})
}
就是这样的原理
后台还需要一个查询,就是根据parentid查询数据,就可以了
一般出现这种情况,往往是这个宽度的问题。宽度没有写死,正常的时候的一级菜单的宽度和hover状态下一级菜单的宽度不同,所以会掉下来。
下面有一个二级菜单的demo,可以对照着找一下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html charset=utf-08" />
<title>dropline-水平三级横向d出菜单</title>
<style type="text/css">
/* common styling */
.hd3_nav {font-family: arial, sans-serif width:749px height:30px position:relative margin:0 font-size:11px margin:0 background:#fff position:relative}
.hd3_nav ul {padding:0 margin:0 list-style-type: none}
.hd3_nav ul li {float:left border-left:1px solid #eee width:106px}
.hd3_nav ul li a, .hd3_nav ul li a:visited {display:block float:left width:101px text-decoration:none padding:0 0 0 5px height:30px line-height:30px color:#000 background:#c9c9a7 url(../../graphics/drop.gif) bottom right no-repeat}
.hd3_nav ul li ul {display: none}
/* specific to non IE browsers */
.hd3_nav ul li:hover a {color:#fff background:#b3ab79}
.hd3_nav ul li:hover ul {display:block position:absolute width:749px top:30px left:0 background:#b3ab79 color:#fff}
.hd3_nav ul li:hover ul.right_side li {float:right border:0 border-left:1px solid #eee}
.hd3_nav ul li:hover ul.left_side li {float:left border:0 border-left:1px solid #eee}
.hd3_nav ul li:hover ul li a.hide {background:#bd8d5e url(../../graphics/drop2.gif) bottom right no-repeat color:#fff}
.hd3_nav ul li:hover ul li:hover a.hide {background:#b3ab79 color:#fff}
.hd3_nav ul li:hover ul li ul {display: none}
.hd3_nav ul li:hover ul li a {display:block background:##b3ab79 color:#fff}
.hd3_nav ul li:hover ul li a:hover {background:#dfc184 color:#000}
.hd3_nav ul li:hover ul li:hover ul {display:block position:absolute left:0 top:30px color:#000 background:#dfc184}
.hd3_nav ul li:hover ul.right li {float:right}
.hd3_nav ul li:hover ul li:hover a.hide {background:#dfc184 color:#000}
</style>
</head>
<body>
<div class="hd3_nav">
<ul>
<li><a class="hide" href="../hd3_nav/index.html">第1个</a>
<ul>
<li><a href="../hd3_nav/zero_dollars.html" title="The zero dollar ads page">zero dollars</a></li>
<li><a href="../hd3_nav/embed.html" title="Wrapping text around images">wrapping text</a></li>
<li><a href="../hd3_nav/form.html" title="Styling forms">styled form</a></li>
<li><a href="../hd3_nav/nodots.html" title="Removing active/focus borders">active focus</a></li>
<li><a href="../hd3_nav/hover_click.html" title="Hover/click with no active/focus borders">HOVER/CLICK</a></li>
<li><a href="../hd3_nav/shadow_boxing.html" title="Multi-position drop shadow">shadow boxing</a></li>
<li><a href="../hd3_nav/old_master.html" title="Image Map for detailed information">image map</a></li>
</ul>
</li>
<li><a class="hide" href="index.html">第2个</a>
<ul>
<li><a href="spies.html" title="a coded list of spies">spies hd3_nav</a></li>
<li><a href="vertical.html" title="a horizontal vertical hd3_nav">vertical hd3_nav</a></li>
<li><a href="expand.html" title="an enlarging unordered list">enlarging list</a></li>
<li><a href="enlarge.html" title="an unordered list with link images">link images</a></li>
<li><a href="cross.html" title="non-rectangular links">non-rectangular</a></li>
<li><a href="jigsaw.html" title="jigsaw links">jigsaw links</a></li>
<li><a href="circles.html" title="circular links">circular links</a></li>
</ul>
</li>
<li><a class="hide" href="../layouts/index.html">第3个</a>
<ul>
<li><a href="../layouts/bodyfix.html" title="Cross browser fixed layout">Fixed 1</a></li>
<li><a href="../layouts/body2.html" title="Cross browser fixed layout">Fixed 2</a></li>
<li><a href="../layouts/body4.html" title="Cross browser fixed layout">Fixed 3</a></li>
<li><a href="../layouts/body5.html" title="Cross browser fixed layout">Fixed 4</a></li>
<li><a href="../layouts/minimum.html" title="A simple minimum width layout">minimum width</a></li>
</ul>
</li>
<li><a class="hide" href="../boxes/index.html">第4个</a>
<ul>
<li><a href="spies.html" title="a coded list of spies">spies hd3_nav</a></li>
<li><a href="vertical.html" title="a horizontal vertical hd3_nav">vertical hd3_nav</a></li>
<li><a href="expand.html" title="an enlarging unordered list">enlarging list</a></li>
<li><a href="enlarge.html" title="an unordered list with link images">link images</a></li>
<li><a href="cross.html" title="non-rectangular links">non-rectangular</a></li>
<li><a href="jigsaw.html" title="jigsaw links">jigsaw links</a></li>
<li><a href="circles.html" title="circular links">circular links</a></li>
</ul>
</li>
<li><a class="hide" href="../mozilla/index.html">第5个</a>
<ul>
<li><a href="../mozilla/dropdown.html" title="A drop down hd3_nav">drop down hd3_nav</a></li>
<li><a href="../mozilla/cascade.html" title="A cascading hd3_nav">cascading hd3_nav</a></li>
<li><a href="../mozilla/content.html" title="Using content:">content:</a></li>
<li><a href="../mozilla/moxbox.html" title=":hover applied to a div">mozzie box</a></li>
<li><a href="../mozilla/rainbow.html" title="I can build a rainbow">rainbow box</a></li>
<li><a href="../mozilla/snooker.html" title="Snooker cue">snooker cue</a></li>
<li><a href="../mozilla/target.html" title="Target Practise">target practise</a></li>
</ul>
</li>
<li><a class="hide" href="../ie/index.html">第6个</a>
<ul class="right_side">
<li><a href="../ie/exampleone.html" title="Example one">example one</a></li>
<li><a href="../ie/weft.html" title="Weft fonts">weft fonts</a></li>
<li><a href="../ie/exampletwo.html" title="Vertical align">vertical align</a></li>
</ul>
</li>
<li><a class="hide" href="../opacity/index.html">第6个</a>
<ul class="right_side">
<li><a href="../opacity/colours.html" title="colour wheel">opaque colours</a></li>
<li><a href="../opacity/picturehd3_nav.html" title="a hd3_nav using opacity">opaque hd3_nav</a></li>
<li><a href="../opacity/png.html" title="partial opacity">partial opacity</a></li>
<li><a href="../opacity/png2.html" title="partial opacity II">partial opacity II</a></li>
<li><a href="../hd3_nav/hover_click.html" title="Hover/click with no active/focus borders">HOVER/CLICK</a></li>
</ul>
</li>
</ul>
</div>
</body>
</html>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)