2010-06-15 25 views
0
$(document).ready(function(){ 

     $('#content1').hide(); 

      $('a#open').click(function(){ 

      $('#content1').show('slow'); 

    }); 

    $('a#close').click(function(){ 
     $('#content1').hide('slow'); 
     }) 

     }); 
中的toogle開關按鈕的這段代碼

是不同如何在此jQuery代碼中打開和關閉div的一個按鈕?

<a><a id="close">

我想使同一按鈕<a>的開啓和關閉,並希望給不同的背景圖像打開和關閉位置

回答

6
$('a').click(function(){ 
    $('#content1').slideToggle(); 
}); 

如果你想改變它們的CSS,你可以按照以下方式重新編寫它。

$('a').click(function(){ 

     if ($('#content1').is(":visible")) 
     { 
      //change your css here 
      //for eg. $(#content1).css('background-image', first_image); 
      $('#content1').hide('slow'); 
     } 
     else{ 
      //change your css here 
      //for eg. $(#content1).css('background-image', second_image); 
      $('#content1').show('slow'); 
     } 

});