最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery slide hide and show same button - Stack Overflow

programmeradmin1浏览0评论

I have this simple jquery slide that I want to have only one button. If the div is being shown, the show button will bee a hide button. And if the div is hidden, the hide button will bee a show button.

Currently, mine has a separate show and hide buttons. I don't know how to merge this buttons into one. I hope someone here can help me. :(

    <script type="text/javascript" 
    src=".10.2.js"></script>
    <script type="text/javascript" 
    src=".10.4/jquery-ui.js"></script>
    <script type="text/javascript" language="javascript">

     $(document).ready(function() {

       $("#hide").click(function(){
          $(".target").hide( "slide", 
                      { direction: "up"  }, 500 );
       });

       $("#show").click(function(){
          $(".target").show( "slide", 
                       {direction: "up" }, 500 );
       });

    });
    </script>
    <style>

      div{ width:100%; 
             height:100px; 
             background: #000; 
             color: #fff; 
         }
   </style>
 </head>

 <div class="target" style="display:none;">
 My Bag Items
 <button id="hide">Hide My Bag</button>
 </div>
 <button id="show">My Bag</button> 

I have this simple jquery slide that I want to have only one button. If the div is being shown, the show button will bee a hide button. And if the div is hidden, the hide button will bee a show button.

Currently, mine has a separate show and hide buttons. I don't know how to merge this buttons into one. I hope someone here can help me. :(

    <script type="text/javascript" 
    src="http://code.jquery./jquery-1.10.2.js"></script>
    <script type="text/javascript" 
    src="http://code.jquery./ui/1.10.4/jquery-ui.js"></script>
    <script type="text/javascript" language="javascript">

     $(document).ready(function() {

       $("#hide").click(function(){
          $(".target").hide( "slide", 
                      { direction: "up"  }, 500 );
       });

       $("#show").click(function(){
          $(".target").show( "slide", 
                       {direction: "up" }, 500 );
       });

    });
    </script>
    <style>

      div{ width:100%; 
             height:100px; 
             background: #000; 
             color: #fff; 
         }
   </style>
 </head>

 <div class="target" style="display:none;">
 My Bag Items
 <button id="hide">Hide My Bag</button>
 </div>
 <button id="show">My Bag</button> 
Share Improve this question asked May 15, 2014 at 18:16 jehzlaujehzlau 6272 gold badges9 silver badges21 bronze badges 1
  • Ooops. never mind I already figured it out. Thanks! :D – jehzlau Commented May 15, 2014 at 18:27
Add a ment  | 

2 Answers 2

Reset to default 5
$("#toggle").click(function(){
    var $target = $('.target'),
        $toggle = $(this);

    $target.slideToggle( 500, function () {
        $toggle.text(($target.is(':visible') ? 'Hide' : 'Show') + ' My Bag');
    });
});

Demo : http://jsfiddle/qmK26/

New markup -

<div class="target">My Bag Items</div>
<button id="togButton">Hide</button>

jQuery

$('#togButton').click(function() {
    $('.target').slideToggle(500);
    if( $(this).text() == 'Hide' ) {
        $(this).text('Show');
    } else {
        $(this).text('Hide');
    }
});
发布评论

评论列表(0)

  1. 暂无评论