简体   繁体   中英

Hide div when other div is clicked

How do I toggle so when I have one div open and I click the other one it closes? Here is my Javascript:

$(function () {
    $('.expander').live('click', function () {
        $('#TableData').slideToggle();
        return false;
    });
     $('.expander2').live('click', function () {
        $('#TableData2').slideToggle();
        return false;
    });
});

 $(document).ready(function(){ $("#TableData").hide(); $(".expander").show(); $('.expander').click(function(){ $("#TableData").slideToggle(); return false; }); $("#TableData2").hide(); $(".expander2").show(); $('.expander2').click(function(){ $("#TableData2").slideToggle(); return false; }); });
 #TableData2 { display: none; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class=expander>Expander</button> <button class=expander2>Expander 2</button> <hr> <div id=TableData>TableData1</div> <div id=TableData2>TableData2</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM