解决方案 »

  1.   

    window.onload = function(){
    var box = document.getElementById('box'),
    h3s = box.getElementsByTagName('h3'),
    divs = box.getElementsByTagName('div');
    for(var i=0;i<h3s.length;i++){
    (function(){
    var j = i;
    h3s[i].onclick = function(){
    toggle(divs[j]);
    }
    })()
    }
    function toggle(e){
    e.style.display=="none" ? e.style.display="block" : e.style.display ="none";
    }
    }
      

  2.   

    发代码,也好过截图呀。
    <h3>一</h3> 
    <div>111</div> 
    <h3>二</h3> 
    <div>222</div> 
    <h3>三</h3> 
    <div>333</div>
    <script>
    var aH3 = document.getElementsByTagName("h3");
    for(var i=0; i<aH3.length; i++){
    aH3[i].onclick = function(){
    this.nextSibling.nextSibling.style.display = (this.nextSibling.nextSibling.style.display && this.nextSibling.nextSibling.style.display=="none")?"block":"none";
    }
    }
    </script>
      

  3.   

    要是我加上一个缓冲运动呢?
    比如:function getStyle(attr,obj){
     if(typeof getComputedStyle=="function"){
       return window.getComputedStyle(obj,null)[attr];
     }else{
       return obj.currentStyle[attr];
     }
    }
    function startMove(obj,json){
    clearInterval(obj.timer);
        obj.timer=setInterval(function(){
    var flag=true;
    for(var attr in json){
         if(attr=="opacity"){
    var temp=parseInt(parseFloat(getStyle(attr,obj))*100);
           }else{
        var temp=parseInt(getStyle(attr,obj));
       }
      var speed=(json[attr]-temp)/6;
          speed=speed>0?Math.ceil(speed):Math.floor(speed);
        if(temp!=json[attr]){
                flag=false;
    }
    if(attr=="opacity"){
          obj.style.opacity=(temp+speed)/100;
          obj.style.filter='alpha(opacity='+(temp+speed)+')';
         }else{
           obj.style[attr]=temp+speed+'px';
     }
                }
    if(flag){
     clearInterval(obj.timer);
    }
    },30);
    }
    window.onload = function(){
        var box = document.getElementById('box'),
        h3s = box.getElementsByTagName('h3'),
        divs = box.getElementsByTagName('div');
        for(var i=0;i<h3s.length;i++){
            (function(){
                var j = i;
                h3s[i].onclick = function(){
                    toggle(divs[j]);
                }
            })()
        }
        function toggle(e){
            // e.style.display=="none" ? e.style.display="block" : e.style.display ="none";
            var h=parseInt(getStyle("height",e));
            h=h==0?50:0;
            startMove(e,{
             height:h
            })
        }
    }上面的我就是根据你的代码改写的有没有更加优化的代码???
      

  4.   

    要是我加上一个缓冲运动呢?
    比如:function getStyle(attr,obj){
     if(typeof getComputedStyle=="function"){
       return window.getComputedStyle(obj,null)[attr];
     }else{
       return obj.currentStyle[attr];
     }
    }
    function startMove(obj,json){
    clearInterval(obj.timer);
        obj.timer=setInterval(function(){
    var flag=true;
    for(var attr in json){
         if(attr=="opacity"){
    var temp=parseInt(parseFloat(getStyle(attr,obj))*100);
           }else{
        var temp=parseInt(getStyle(attr,obj));
       }
      var speed=(json[attr]-temp)/6;
          speed=speed>0?Math.ceil(speed):Math.floor(speed);
        if(temp!=json[attr]){
                flag=false;
    }
    if(attr=="opacity"){
          obj.style.opacity=(temp+speed)/100;
          obj.style.filter='alpha(opacity='+(temp+speed)+')';
         }else{
           obj.style[attr]=temp+speed+'px';
     }
                }
    if(flag){
     clearInterval(obj.timer);
    }
    },30);
    }
    window.onload = function(){
        var box = document.getElementById('box'),
        h3s = box.getElementsByTagName('h3'),
        divs = box.getElementsByTagName('div');
        for(var i=0;i<h3s.length;i++){
            (function(){
                var j = i;
                h3s[i].onclick = function(){
                    toggle(divs[j]);
                }
            })()
        }
        function toggle(e){
            // e.style.display=="none" ? e.style.display="block" : e.style.display ="none";
            var h=parseInt(getStyle("height",e));
            h=h==0?50:0;
            startMove(e,{
             height:h
            })
        }
    }上面的我就是根据你的代码改写的有没有更加优化的代码???
    直接用jq的特效