小弟新手,在网上找了个JS代码
默认为展开的,我想改成默认为收缩的
希望大哥们帮忙,小弟感激不尽!
代码如下:<script type="text/javascript">
<!--
var mh = 28;//最小高度 
var step = 5;//每次变化的px量 
var ms = 10;//每隔多久循环一次 
function toggle(o){ 
if (!o.tid)o.tid = "_" + Math.random() * 80; 
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; 
}
document.getElementById("aa").innerText = "+ 点击展开";
document.getElementById("aa").textContent = "+ 点击展开";

else{ 
if (o.offsetHeight >= t.maxHeight){ 
clearTimeout(t.timer); 
return; 
}
document.getElementById("aa").innerText = "- 点击收缩";
document.getElementById("aa").textContent = "- 点击收缩";

o.style.height = (parseInt(o.style.height, 10) + t.action * step) + "px"; 
window.toggler[id].timer = setTimeout("anim('"+id+"')",ms ); 
}
//-->
</script>
<style>
.right_top_box {
width: 100%;
height:180px;
border: 1px solid #2e2e2e;
overflow: hidden;
}
.right_top_left { float:left;}
.right_top_right { float:right;}
</style>
<div class="right_top_box">
<div class="right_top_nav" onclick="toggle(this.parentNode)">
<div class="right_top_left">
内容!!!</div>
<div class="right_top_right"><span color:#990622;" id="aa">- 点击收缩</span>
</div>
        </div>
      </div>

解决方案 »

  1.   


    <script type="text/javascript"> 
    <!-- 
    var mh = 28;//最小高度 
    var step = 5;//每次变化的px量 
    var ms = 10;//每隔多久循环一次
    var maxh 
    function toggle(o){
    if (!o.tid)o.tid = "_" + Math.random() * 80; 
    if (!window.toggler)window.toggler = {}; 
    if (!window.toggler[o.tid]){ 
    window.toggler[o.tid]={ 
    obj:o, 
    maxHeight:maxh,
    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; 

    document.getElementById("aa").innerText = "+ 点击展开"; 
    document.getElementById("aa").textContent = "+ 点击展开"; 

    else{ 
    if (o.offsetHeight >= t.maxHeight){ 
    clearTimeout(t.timer); 
    return; 

    document.getElementById("aa").innerText = "- 点击收缩"; 
    document.getElementById("aa").textContent = "- 点击收缩"; 

    o.style.height = (parseInt(o.style.height, 10) + t.action * step) + "px"; 
    window.toggler[id].timer = setTimeout("anim('"+id+"')",ms ); 

    window.onload=function(){
    document.getElementById("aa").innerText = "+ 点击展开"; 
    document.getElementById("aa").textContent = "+ 点击展开"; 
    maxh = document.getElementById("main").offsetHeight;
    document.getElementById("main").style.height = mh;
    }
    //--> 
    </script> 
    <style> 
    .right_top_box { 
    width: 100%; 
    height:180px; 
    border: 1px solid #2e2e2e; 
    overflow: hidden; 

    .right_top_left { float:left;} 
    .right_top_right { float:right;} 
    </style> 
    <div id="main" class="right_top_box"> 
    <div class="right_top_nav" onclick="toggle(this.parentNode)"> 
    <div class="right_top_left"> 
    内容!!! </div> 
    <div class="right_top_right"> <span color:#990622;" id="aa">- 点击收缩 </span> 
    </div> 
            </div> 
          </div>
      

  2.   

    非常感谢楼上的朋友“hookee”帮忙!
    多谢了哦!
      

  3.   

    用jquery吧,你这样子写的代码也太多冗长了呀。