我希望得到图片中的效果,一打开页面,第一个层展开,后面的层全部收缩(需要点击再展开);
但目前的代码的效果是一打开页面全部展开。菜鸟,不太会JS,求指教
以下为全部代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>内容收起、展开的JS/CSS特效代码</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Author" content="Sheneyan" />
<script type="text/javascript">
var mh = 87;//收缩以后的最小高度
var step = 10;//每次变化的px量,控制速度,数字越大越快
var ms = 10;//每隔多久循环一次,缓冲速度,数字越大越慢
function toggle(o){
  if (!o.tid)o.tid = "_" + Math.random() * 100;
  if (!window.toggler)window.toggler = {};
  if (!window.toggler[o.tid]){
    window.toggler[o.tid]={
      obj:o,
      maxHeight:o.offsetHeight,
      minHeight:mh,
      timer:null,
      action:1
    };
  }
  o.style.height = o.offsetHeight + "px";
  if (window.toggler[o.tid].timer)clearTimeout(window.toggler[o.tid].timer);
  window.toggler[o.tid].action *= -1;
  window.toggler[o.tid].timer = setTimeout("anim('"+o.tid+"')",ms );
}
function anim(id){
  var t = window.toggler[id];
  var o = window.toggler[id].obj;
  if (t.action < 0){
    if (o.offsetHeight <= t.minHeight){
      clearTimeout(t.timer);
      return;
    }
  }
  else{
    if (o.offsetHeight >= t.maxHeight){
      clearTimeout(t.timer);
      return;
    }
  }
  o.style.height = (parseInt(o.style.height, 10) + t.action * step) + "px";
  window.toggler[id].timer = setTimeout("anim('"+id+"')",ms );
}
</script>
<style type="text/css">
.main{
margin:20px;
overflow:hidden;
}
.main .wrap{
overflow:hidden;
}
.main .wrap .head{
border:0px;
margin:0;
padding:0;
height:76px;
width:692px;
cursor:pointer;
background-image:url(images/1.gif);
background-repeat:no-repeat;
text-align:center;
}
.main .wrap .head h2{
line-height:30px;
text-align:center;
padding-top: 20px;
padding-right: 0;
padding-bottom: 0;
padding-left: 0;
}
.main .wrap .content{
border:0px;
overflow:hidden;
background-image:url(images/2.gif);
background-repeat:repeat-y;
width:621px;
margin-left:35px;
}
.main .wrap .content p {
margin-left:70px;
padding:0;
}
.main .footer{
border:0px;
padding:0;
margin:0;
height:68px;
width:692px;
background-image:url(images/3.gif);
background-repeat:no-repeat;
}
</style>
</head>
<body>
<div class="main">
  <div class="wrap">
   <div class="head" onclick="toggle(this.parentNode)"><h2>伸缩效果</h2></div>
   <div class="content">
    <p>内容</p>
    <p>内容</p>
    <p>内容</p>
    <p>内容</p>
   </div>
  </div>
  <div class="footer"></div >
</div><div class="main">
  <div class="wrap">
   <div class="head" onclick="toggle(this.parentNode)"><h2>伸缩效果</h2></div>
   <div class="content">
    <p>内容</p>
    <p>内容</p>
    <p>内容</p>
    <p>内容</p>
   </div>
  </div>
  <div class="footer"></div >
</div>
</body>
</html>