简体   繁体   中英

jQuery sliding a ul up and down when heading is clicked

For my page I have headings with a class of "head". When I click on them they are supposed to show the ul class of "content" under them, and when a new header is clicked, the list currently showing disappears and the new list for the new header appears. I have that down, but say I click header 1 and it shows list one. When I click it again, I need the list to slide back up, but I can't get it to be like that. This is what I have so far:

<!DOCTYPE html>
<html lang="en">
<head>
<title>FV Runners</title>
<meta charset="UTF-8">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <link href="styles/normalize.css" rel="stylesheet" />
  <link href="styles/my_style.css" rel="stylesheet" />
  <script>
  $(document).ready(function(){
    $(".head").on("click", function(){
      $(".content").hide();
      $(this).next(".content").slideToggle();
    })
  })
  </script>
</head>
<body>
  <div id="header">
    <h1>Fox Valley Runners Club</h1>
  </div> <!-- End of 'header' div-->

  <div id="main">
  </div>  <!-- End of 'main' div-->

  <div id="pics">

    <div class="race_box">
      <img src="images/run1.jpg" /><br />
      <figcaption>5k/10k Events</figcaption>

      <div class=".races" id="5k">
        <h3>5k/10 Events</h3>
        <div>
          <h4 class="head">Mini Sprint</h4>
            <ul class="content">
              <li>10/30/20<br>Memorial Park<br>Appleton</li>
            </ul>
          <h4 class="head">Iron Horse</h4>
          <ul class="content">
            <li>11/06/20<br>Bay Beach Park<br>Green Bay</li>
          </ul>
          <h4 class="head">Twilight Trail</h4>
          <ul class="content">
            <li>11/13/20<br>>River's Edge Park<br>Wrightstown</li>
          </ul>
        </div>
      </div><!--  End of '5k' div-->
    </div> <!-- End of 'run1' div-->

    <div class="race_box">
      <img src="images/run2.jpg" /><br />
      <figcaption>Half Marathon Events</figcaption>

      <div class=".races" id="half">
        <h3>Half Marathon Events</h3>
        <div>
          <h4 class="head">Fox River Marathon</h4>
            <ul class="content">
              <li>10/15/20<br>Pierce Park<br>Appleton</li>
            </ul>
          <h4 class="head">N.E.W. Half Marathon</h4>
            <ul class="content">
              <li>10/29/20<br>Bay Beach Park<br>Green Bay</li>
            </ul>
          <h4 class="head">Winnebago Run</h4>
            <ul class="content">
              <li>11/27/20<br>Menominee Park<br>>Oshkosh</li>
            </ul>
        </div>
      </div> <!-- End of 'half' div-->
    </div><!-- End of 'run2' div-->

    <div class="race_box">
      <img src="images/run3.jpg" /><br />
      <figcaption>Full Marathon Events</figcaption>

      <div class=".races "id="full">
        <h3>Full Marathon Events</h3>
        <div>
          <h4 class="head">Cheesehead Marathon</h4>
            <ul class="content">
              <li>9/24/20<br>Pamperin Park<br>Green Bay</li>
            </ul>
          <h4 class="head">Chain O'Lakes Marathon</h4>
            <ul class="content">
              <li>10/29/20<br>Bay Beach Park<br>Green Bay</li>
            </ul>
          <h4 class="head">Fox Cities Marathon</h4>
            <ul class="content">
              <li>11/12/20<br>Menominee Park<br>>Oshkosh</li>
            </ul>
        </div>
      </div> <!-- End of 'full' div-->
    </div> <!-- End of 'run3' div-->

  </div> <!-- End of 'pics' div-->


</body>
</html>

I need help with getting the list for the header it is under to slide up when I click the same header.

You simply need to exclude the current item form the selection you call hide on. The not method works well for this.

 $(document).ready(function() { $(".head").on("click", function() { let $list = $(this).next(".content"); $(".content").not($list).hide(); $list.slideToggle(); }) })
 .content { display: none; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h4 class="head">Mini Sprint</h4> <ul class="content"> <li>10/30/20<br>Memorial Park<br>Appleton</li> </ul> <h4 class="head">Iron Horse</h4> <ul class="content"> <li>11/06/20<br>Bay Beach Park<br>Green Bay</li> </ul> <h4 class="head">Twilight Trail</h4> <ul class="content"> <li>11/13/20<br>>River's Edge Park<br>Wrightstown</li> </ul>

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