简体   繁体   中英

Slide Up/Down with jQuery

I need to slide up/down or just show and hide div but it's not working. My code:

<div class="facilities">
            <div id="facheader">
                <div class="facheadname">
                    Facilities
                </div>
                <div class="facheaderbutton">
                    &#9651;
                </div>
            </div>
            <div id="factable">
                <table border="0">
                         <tr><div class="factableheader">
                         <th>Name</th><th>City</th><th>Country</th><th>Compliance Certification</th><th>Audit History</th><th>Date</th><th>Remediation History</th><th>Date</th></div>
                         </tr>
                         <tr>
                         <td>Kowloon</td><td>Hong Kong</td><td>Hong Kong</td><td>cGMP-FDA</td><td>Compliant cGMP-SeerPharma</td><td>12/12/10</td><td>Clean room staff training – IRB-C</td><td>01/03/05</td>
                         </tr>
                 </table>
            </div>
        </div>

I tried code - it's hiding content but not showing when pressed again:

$('#facheader').click(function(){
            if ($('#factable').is(':hidden')){
                $('#factable').show();}
            else{
                $('.contclickedinfo').hide();
            }
            return false;
        });



        $('#factable').click(function(e) {
            e.stopPropagation();
        });
        $(document).click(function() {
            $('#factable').hide();
        });

I also tried these but they are not working at all:

$(document).ready(function(){
    $('#facheader').click(function(){
        $('#factable').slideUp(), function(){
        $('#factable').slideDown();
    });
});

and

$('#facheader').toggle(function(){ 

        $('#factable').slideUp(800);  // Text slides up 
        }, function(){ 

        $('#factable').slideDown(800); // Text slides down
    }); 
$('#facheader').click(function() { 
    if ($('#factable').is(':visible'))
        $('#factable').slideUp(800);  // Text slides up 
    else
        $('#factable').slideDown(800); // Text slides down
}); 
$('#facheader').click(function() {
    if ($('#factable').is(':visible')){
        $('#factable').hide();
    } else {
        $('#factable').show();
    }

    return false;
});

That should work just the way you want to. I changed the element you wanted to hide, because you picked a different element. Besides, the :visible selector has served me better than :hidden . I'm not sure why :hidden doesn't always work the way we expect it to work.

Short with toggle function

$("#facheader").click(function(){
    $("#factable").slideToggle(); /* between () you can define speed in MS */
});

Because it's not a link that triggers the slide, you don't need to return false or stopPropagation.

Something which is strange is:

<tr><div class="factableheader">

When browsers see this, they will put your outside your table.

Are you using multiple DIV's with the same ID?

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