简体   繁体   中英

toggle() hide/show with different element

I got a list table, so when I click one of list table on it, another list show. and i click back, list will be hide. here is the code for the hide and show.

$(document).ready(function() {
  //Hide table rows with class 'min', but appear when clicked.
  $(".data").hide();
  $(".main").click(function() {
    $(this).parent().parent().next(".data").toggle();
  });

});

but, when i click back. can't hide. Please correct me. Thanks

Check my example

DEMO

If the .main clicked has a parent with class .data , then it means click was on a new shown row. So we find the parent div and close it. Else we will find and hide/show the immediate next row.

$(document).ready(function () {
    //Hide table rows with class 'min', but appear when clicked.
    $(".data").hide();
    $(".main").click(function () {
        if($(this).parents('.data').length)
            $(this).closest('.data').toggle();
        else
            $(this).next(".data").toggle();
    });
});

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