2013-03-08 332 views
0

所以我发现了一个脚本,可以很好地显示一个隐藏的div,只要点击我选择的另一个元素。但是,我希望这更像一个切换。谁能帮我吗?我试过尽我所能调整脚本,但jQuery并不是我的特长。jQuery - 点击隐藏我已经点击显示的元素

<script type="text/javascript"> 
    (function($){ 
    document.documentElement.className += " js"; // Add js class to the HTML element 
    $(function(){ 
     var $containers = $("#repaircontent").hide(); 

     jQuery(document).ready(function(){ 
     /* Show the HTML page only after the js and css are completely loaded */ 
     delayShow(); 
     }); 

     function delayShow() { 
     var secs = 1000; 
      setTimeout('jQuery("#repaircontent").css("visibility","visible");', secs); 
     } 

     $('#repairtab').each(function(i,el){ 
     var idx = i; 
     $(this).click(function(e){ 
      var $target = $containers.filter(':eq(' + idx + ')'); 
      $target.not(':visible').fadeIn(); 
      e.preventDefault(); 

     }) 
     }) 
    }) 
    })(jQuery); 
</script> 
+1

有你,尽管使用切换的? http://api.jquery.com/toggle/ – Drakoumel 2013-03-08 14:47:38

回答

3

你试过jquery的切换吗? http://api.jquery.com/toggle/

+0

我如何在上面的代码中应用切换?我认为使用切换教程为这个脚本编写一个新脚本可能会更容易:) – 2013-03-08 14:51:36

1

只需更换淡入与切换...

 $('#repairtab').each(function(i,el){ 
    var idx = i; 
    $(this).click(function(e){ 
     var $target = $containers.filter(':eq(' + idx + ')'); 
     $target.toggle(); 
     e.preventDefault(); 

    }) 
    }) 
}) 
})(jQuery); 
+0

足够简单,我的jQuery语法不足。感谢您的快速帮助。这个伎俩。 – 2013-03-08 15:02:20