向下滑动按钮以显示文本HTML CSS

时间:2018-08-17 12:45:34

标签: jquery html css slidedown

我正在尝试使我的网站上的按钮向下滑动以显示文本,但是目前它仅显示按钮及其下面的文本。该按钮除了单击外不执行任何操作。我希望单击按钮后在底部显示文本。我使用了.slideDown(),但它只是使按钮显示而不是赋予按钮任何功能。

    <html>
    <head>
        <style>
            body {
                background: rgb(230, 230, 255);
                color: rgb(0, 0, 0);
                padding: 10px;
                font-family: Georgia;
            }
            header {
                font-size: 1.5em;
                font-weight: bold;
            }
            [id=all-contents] {
                max-width: 800px;
                margin: auto;
            }
    
            /* navigation menu */
            nav
             {
                background: rgb(239, 80, 41);
                margin: 0 auto;
                margin-bottom: 20px;
                display: flex;
                padding: 10px;
                border-radius:2em;
                border-top-left-radius:2em;
                border-top-right-radius:2em;
                border-bottom-right-radius:2em;
                border-bottom-left-radius:2em;
            }
            nav header {
                display: flex;
                align-items: center;
                color: rgb(255, 255, 255);
                flex: 1;
            }
            nav ul {
                list-style-image: none;
            }
            nav li {
                display: inline-block;
                padding: 0 10px;
            }
            nav a {
                text-decoration: none;
                color: #fff;
            }
    
            /* main container area beneath menu */
            main {
                background: rgb(245, 238, 219);
                display: flex;
            }
            [class=sidebar] {
                margin-right: 25px;
                padding: 10px;
            }
            [class=sidebar] img {
                width: 200px;
            }
            [class=content] {
                flex: 1;
                padding: 15px;
            }
            [class=interests] header {
                font-size: 1.25em;
            }
        </style>
        <title>Website</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        
        <script>
            $(document).ready(function(){
                $("button").click(function(){
                    $("ul.professional interests").slideDown();
                });
            });
        </script>
    </head>
    
    <body>
        <div id="all-contents">
            <nav>
                <header>Website</header>
                <ul>
                    <li><a href="index.html">Home</a></li>
                    <li><a href="portfolio.html">Portfolio</a></li>
                </ul>
            </nav>
            
            <main>
                <div class="sidebar">
                
                    <img src="https://mail.google.com/mail/u/0/?ui=2&ik=4df09cafb2&view=fimg&th=165452a04091804b&attid=0.1.1&disp=emb&attbid=ANGjdJ9lewmMheGHuQrx4zaTH2buVPilOzWqvInJBZxDZsnva2ziG_NFaDFjBtTzmraafUlg5Km7HQSkwzmjRtgVV3FtzYRjLGvV9B2gueoBBfFmAO60Fhr9h-LAmhQ&sz=s0-l75-ft&ats=1534463719799&rm=165452a04091804b&zw&atsh=1">
                </div>
                
                <div class="content">
                    <header> </header>
                    <p> Student</p>
                <section class="interests">
                    <header>Very Interesting Interests</header>
                    <ul>
                        <li>Reading</li>
                        <li>Coding</li>
                        <li>Writing</li>
                    </ul>
                </section>
                    </main>
                <main class="container2">
                      <div class="professional-content">
                          <header>About Me</header>
                          <section class = "interests">
                              <button> My professional interests</button>
                              <ul class = "professional interests">
                                <li> Political Analysis </li>
                                <li> Computer Science </li>
                              </ul>
                              <button> My Resume </button>
                              <ul class = "My Resume">
                                  <li> <b>IBCs:</b> </li>
                                  <li> <b>Act Scores:</b> </li>
                                  <li> <b>Classes:</b></li>
                                  <li> <b>Extracurriculars:</b> </li>
                                  <li> <b>Future Goals:</b> </li>
                              </ul>
                        </section>
                  </div>
            </main>
        </div>
    </body>
</html>

2 个答案:

答案 0 :(得分:2)

一些更正,以及对我试图达到的目标的解决方案:

  • 不要在CSS中使用[class=interests],其句点是这样的:.interests
  • 不要在CSS中使用[id=all-contents]#是为此而设计的:#all-contents
  • 不要在类名中使用空格! professional interestsprofessional_interests
  • HTML中缺少结束符</div>
  • 然后,在脚本中,可以在ul之后使用.next('ul')定位button

代码段

// Modified the script
$(".professional-content button").click(function() {
  $(this).next('ul').slideDown();
});
body {
  background: rgb(230, 230, 255);
  color: rgb(0, 0, 0);
  padding: 10px;
  font-family: Georgia;
}

header {
  font-size: 1.5em;
  font-weight: bold;
}

#all-contents {
  max-width: 800px;
  margin: auto;
}


/* navigation menu */

nav {
  background: rgb(239, 80, 41);
  margin: 0 auto;
  margin-bottom: 20px;
  display: flex;
  padding: 10px;
  border-radius: 2em;
  border-top-left-radius: 2em;
  border-top-right-radius: 2em;
  border-bottom-right-radius: 2em;
  border-bottom-left-radius: 2em;
}

nav header {
  display: flex;
  align-items: center;
  color: rgb(255, 255, 255);
  flex: 1;
}

nav ul {
  list-style-image: none;
}

nav li {
  display: inline-block;
  padding: 0 10px;
}

nav a {
  text-decoration: none;
  color: #fff;
}


/* main container area beneath menu */

main {
  background: rgb(245, 238, 219);
  display: flex;
}

.sidebar {
  margin-right: 25px;
  padding: 10px;
}

.sidebar img {
  width: 200px;
}

.content {
  flex: 1;
  padding: 15px;
}

.interests header {
  font-size: 1.25em;
}

/* TAKIT: Added the below */

button {
  display: block; /* To avoid inline */
}

.professional-content ul {
  display: none;
  transition: 500ms all ease;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="all-contents">
  <nav>
    <header>Ani's Amazing Website</header>
    <ul>
      <li><a href="index.html">Home</a></li>
      <li><a href="portfolio.html">Portfolio</a></li>
    </ul>
  </nav>

  <main>
    <div class="sidebar">
      <img src="https://mail.google.com/mail/u/0/?ui=2&ik=4df09cafb2&view=fimg&th=165452a04091804b&attid=0.1.1&disp=emb&attbid=ANGjdJ9lewmMheGHuQrx4zaTH2buVPilOzWqvInJBZxDZsnva2ziG_NFaDFjBtTzmraafUlg5Km7HQSkwzmjRtgVV3FtzYRjLGvV9B2gueoBBfFmAO60Fhr9h-LAmhQ&sz=s0-l75-ft&ats=1534463719799&rm=165452a04091804b&zw&atsh=1">
    </div>

    <div class="content">
      <header> Ani Gonzales </header>
      <p> Northshore Student</p>
      <section class="interests">
        <header>Very Interesting Interests</header>
        <ul>
          <li>Reading</li>
          <li>Coding</li>
          <li>Writing</li>
        </ul>
      </section>
    </div><!-- TAKIT: Added, it was missing -->
  </main>
  <main class="container2">
    <div class="professional-content">
      <header>About Me</header>
      <section class="interests">
        <button>My professional interests</button>
        <ul class="professional_interests">
          <li> Political Analysis </li>
          <li> Computer Science </li>
        </ul>
        <button>My Resume</button>
        <ul class="My Resume">
          <li> <b>IBCs:</b> </li>
          <li> <b>Act Scores:</b> </li>
          <li> <b>Classes:</b></li>
          <li> <b>Extracurriculars:</b> </li>
          <li> <b>Future Goals:</b> </li>
        </ul>
      </section>
    </div>
  </main>
</div>

答案 1 :(得分:1)

我认为,如果您要隐藏单击按钮时要显示的文字,这会容易得多

   <div style="display: none;">My Text</div>

,然后在单击具有此功能的按钮时将其显示:

 function showText() {
 getElementById("textId").style.display="block";

然后将功能添加到您的按钮:

<button onclick="showText()">My Button</button>

我希望这会有所帮助。