如何在这个jquery代码中创建一个用于打开和关闭div的按钮?

Jit*_*yas 0 css xhtml jquery

 $(document).ready(function(){

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

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

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

   });

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

      });
Run Code Online (Sandbox Code Playgroud)

在此代码中,toogle打开和关闭按钮是不同的

<a><a id="close">

我想要<a>打开和关闭相同的按钮,并希望为打开和关闭位置提供不同的背景图像

Kai*_*Kai 6

$('a').click(function(){
  $('#content1').slideToggle();
});
Run Code Online (Sandbox Code Playgroud)

如果您想更改他们的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');
       }

});
Run Code Online (Sandbox Code Playgroud)