js下拉菜单,怎样更简便的实现?

JavaScript023

js下拉菜单,怎样更简便的实现?,第1张

你这个效果的下拉菜单,不需要用 JavaScript,只用CSS就能实现。

加上一句

li:hover ul{ display:block}

就行了。

<!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/htmlcharset=utf-8"/>

<title>无标题文档</title>

<style type="text/css">* {

margin: 0px

padding: 0px

}

body {

font-family: Verdana, Geneva, sans-serif

font-size: 14px

}

#nav {

width: 600px

height: 40px

background-color: #eee

margin: 0 auto

}

ul {

list-style: none

}

ul li {

float: left

line-height: 40px

text-align: center

width: 100px

}

a {

text-decoration: none

color: #000

display: block

}

a:hover {

color: #F00

background-color: #666

}

ul li ul li {

float: none

background-color: #eee

margin: 2px 0px

}

ul li ul {

display: none

}

li:hover ul{ display:block}

</style>

</head>

<body>

<div id="nav">

<ul>

<li><a href="#">首页</a></li>

<li><a href="#">课程大厅</a>

<ul>

<li><a href="#">JavaScript</a></li>

<li><a href="#">Html/CSS</a></li>

</ul>

</li>

<li><a href="#">学习中心</a>

<ul>

<li><a href="#">视频学习</a></li>

<li><a href="#">实例练习</a></li>

<li><a href="#">问与答</a></li>

</ul>

</li>

<li><a href="#">经典案例</a></li>

<li><a href="#">关于我们</a></li>

</ul>

</div>

</body>

</html>

直接写了一下,不知道是不是你想要的,

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=UTF-8">

<title>Insert title here</title>

<script type="text/javascript">

function clickA(){

document.form1.textname.style.display=""

document.form1.selectname.style.display="none"

}

function clickB(){

document.form1.textname.style.display="none"

document.form1.selectname.style.display=""

}

</script>

</head>

<body>

<form name="form1" action="">

<select>

<option onclick="clickA()">A</option>

<option onclick="clickB()">B</option>

</select>

<input type="text" name="textname" style="display:none" >

<select name="selectname" style="display:none">

<option>A</option>

<option>B</option>

</select>

</form>

</body>

</html>