JS如何控制竖直二级目录!点击一次一级目录展开二级菜单!再点击关闭
大家好!我是一个JS小白!自己摸索整理了一个竖直的二级菜单目录!整理中出现的问题!现在的程序点击一级目录可以展开二级菜单目录!但是再点击无法关闭<!DOCTYPEhtml...
大家好!我是一个JS小白!自己摸索整理了一个竖直的二级菜单目录!整理中出现的问题!现在的程序 点击一级目录可以展开二级菜单目录!但是再点击无法关闭
<!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-8" />
<title>无标题文档</title>
<script type="text/javascript">
function DoMenu(num,count)
{
for(var i=1;i<=count;i++)
{
if(num==i)
{
document.getElementById("ChildMenu"+num).className = "liblock";
document.getElementById("liParent"+num).className = "sub_Min";
}
else
{
document.getElementById("ChildMenu"+i).className = "collapsed";
document.getElementById("liParent"+i).className = "sub_and";
}
}
}
</script>
<style type="text/css">
#nav {
font-family: arial, serif;
font-size:12px;
width:150px;
line-height: 24px;
list-style-type: none;
margin:10px;
text-align:left;
}
#nav a {
width: 150px;
display: block;
padding-left:20px;
}
#nav li {
float:left;
color:#DD1336;
font-weight:bold;
}
.sub_and {
background:url(../images/ar.png) no-repeat 3px 3px;
}
.sub_Min {
background:url(../images/ad.png) no-repeat 3px 3px;
}
#nav li a:hover {
}
#nav a:link {
color:#06c;
text-decoration:none;
}
#nav a:visited {
color:#06c;
text-decoration:none;
}
#nav a:hover {
color:#f60;
text-decoration:none;
font-weight:bold;
}
#nav li ul {
list-style:none;
text-align:left;
}
#nav li ul li {
padding:0;
font-weight:normal;
}
#nav li ul a {
padding-left:10px;
width:140px;
}
#nav li ul a:link {
color:#666;
text-decoration:none;
}
#nav li ul a:visited {
color:#666;
text-decoration:none;
}
#nav li ul a:hover {
color:#f60;
text-decoration:none;
font-weight:normal;
}
#nav li:hover ul {
left: auto;
}
#nav li.sfhover ul {
left: auto;
}
#content {
clear: left;
}
#nav ul.collapsed {
display: none;
}
#nav ul.liblock {
display:block;
}
#PARENT {
width:150px;
}
</style>
</head>
<body>
<div id="PARENT">
<ul id="nav">
<li id="liParent1" class="sub_and"><a href="#Menu=ChildMenu1" onClick="DoMenu(1,1)">Advertising</a>
<ul id="ChildMenu1" class="collapsed">
<li><a href=##Menu=ChildMenu1>Advertising1</a></li>
<li><a href=##Menu=ChildMenu1>Advertising2</a></li>
<li><a href=##Menu=ChildMenu1>Advertising3</a></li>
<li><a href=##Menu=ChildMenu1>Advertising4</a></li>
<li><a href=##Menu=ChildMenu1>Advertising5</a></li>
<li><a href=##Menu=ChildMenu1>Advertising6</a></li>
<li><a href=##Menu=ChildMenu1>Advertising7</a></li>
<li><a href=##Menu=ChildMenu1>Advertising8</a></li>
<li><a href=##Menu=ChildMenu1>Advertising9</a></li>
<li><a href=##Menu=ChildMenu1>Advertising10</a></li>
</ul>
</li>
</ul>
</div>
</body>
</html> 展开
<!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-8" />
<title>无标题文档</title>
<script type="text/javascript">
function DoMenu(num,count)
{
for(var i=1;i<=count;i++)
{
if(num==i)
{
document.getElementById("ChildMenu"+num).className = "liblock";
document.getElementById("liParent"+num).className = "sub_Min";
}
else
{
document.getElementById("ChildMenu"+i).className = "collapsed";
document.getElementById("liParent"+i).className = "sub_and";
}
}
}
</script>
<style type="text/css">
#nav {
font-family: arial, serif;
font-size:12px;
width:150px;
line-height: 24px;
list-style-type: none;
margin:10px;
text-align:left;
}
#nav a {
width: 150px;
display: block;
padding-left:20px;
}
#nav li {
float:left;
color:#DD1336;
font-weight:bold;
}
.sub_and {
background:url(../images/ar.png) no-repeat 3px 3px;
}
.sub_Min {
background:url(../images/ad.png) no-repeat 3px 3px;
}
#nav li a:hover {
}
#nav a:link {
color:#06c;
text-decoration:none;
}
#nav a:visited {
color:#06c;
text-decoration:none;
}
#nav a:hover {
color:#f60;
text-decoration:none;
font-weight:bold;
}
#nav li ul {
list-style:none;
text-align:left;
}
#nav li ul li {
padding:0;
font-weight:normal;
}
#nav li ul a {
padding-left:10px;
width:140px;
}
#nav li ul a:link {
color:#666;
text-decoration:none;
}
#nav li ul a:visited {
color:#666;
text-decoration:none;
}
#nav li ul a:hover {
color:#f60;
text-decoration:none;
font-weight:normal;
}
#nav li:hover ul {
left: auto;
}
#nav li.sfhover ul {
left: auto;
}
#content {
clear: left;
}
#nav ul.collapsed {
display: none;
}
#nav ul.liblock {
display:block;
}
#PARENT {
width:150px;
}
</style>
</head>
<body>
<div id="PARENT">
<ul id="nav">
<li id="liParent1" class="sub_and"><a href="#Menu=ChildMenu1" onClick="DoMenu(1,1)">Advertising</a>
<ul id="ChildMenu1" class="collapsed">
<li><a href=##Menu=ChildMenu1>Advertising1</a></li>
<li><a href=##Menu=ChildMenu1>Advertising2</a></li>
<li><a href=##Menu=ChildMenu1>Advertising3</a></li>
<li><a href=##Menu=ChildMenu1>Advertising4</a></li>
<li><a href=##Menu=ChildMenu1>Advertising5</a></li>
<li><a href=##Menu=ChildMenu1>Advertising6</a></li>
<li><a href=##Menu=ChildMenu1>Advertising7</a></li>
<li><a href=##Menu=ChildMenu1>Advertising8</a></li>
<li><a href=##Menu=ChildMenu1>Advertising9</a></li>
<li><a href=##Menu=ChildMenu1>Advertising10</a></li>
</ul>
</li>
</ul>
</div>
</body>
</html> 展开
1个回答
展开全部
<script type="text/javascript">
function DoMenu(num){
if(document.getElementById("ChildMenu"+num).className === "liblock")
{
document.getElementById("ChildMenu"+num).className = "collapsed";
document.getElementById("liParent"+num).className = "sub_and";
}else{
document.getElementById("ChildMenu"+num).className = "liblock";
document.getElementById("liParent"+num).className = "sub_Min";
}
}
</script>
onClick="DoMenu(1)
这个还很简单,不能针对多个下拉的情况,因为你有多个的话,总希望开第一个,其他的全部缩回去。
可以用JQuery框架做,不用打那么长代码了,比如
document.getElementById("ChildMenu"+num)
可以写成$("#ChildMenu"+num),对方便啊,去学学JQuery吧,简单多了
function DoMenu(num){
if(document.getElementById("ChildMenu"+num).className === "liblock")
{
document.getElementById("ChildMenu"+num).className = "collapsed";
document.getElementById("liParent"+num).className = "sub_and";
}else{
document.getElementById("ChildMenu"+num).className = "liblock";
document.getElementById("liParent"+num).className = "sub_Min";
}
}
</script>
onClick="DoMenu(1)
这个还很简单,不能针对多个下拉的情况,因为你有多个的话,总希望开第一个,其他的全部缩回去。
可以用JQuery框架做,不用打那么长代码了,比如
document.getElementById("ChildMenu"+num)
可以写成$("#ChildMenu"+num),对方便啊,去学学JQuery吧,简单多了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询