开发者

How to make one button for open and close div, in this jquery code?

 $(document).ready(function(){

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

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

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

   });

   $('a#close').click(function(){
        $(开发者_JAVA技巧'#content1').hide('slow');
        })

      });

in this code of toogle open and close button is different

<a> and <a id="close">

I want to make same button <a> for open and close and want to give different background image for open and close position


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

If you would like to change their css ,you can re-write it in the following way.

$('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');
       }

});


Have a look at the Toggle section of the API.


Make your own function called ShowOrHide() and call that when the button is clicked.

In ShowOrHide, see if the div is already visible/hidden and do the opposite.

Then you'll only need one button to perform both operations.


Check out this jsFiddle I made a while ago. (With a lot of help from SO)

Hopefully this is what you're looking for.

Hope it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜