You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于W3Schools手风琴教程,设置默认展开第一个Section 1

Hey there! I've put together a complete, self-contained accordion implementation where Section 1 is open by default. It's straightforward with clean HTML, CSS, and JS—let's dive in:

Accordion with Section 1 Default Open

HTML Structure

The key here is marking the first accordion button as active and setting its panel to display: block so it loads open:

<button class="accordion active">Section 1</button>
<div class="panel" style="display: block;">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>

<button class="accordion">Section 2</button>
<div class="panel">
  <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.</p>
</div>

<button class="accordion">Section 3</button>
<div class="panel">
  <p>Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet.</p>
</div>

CSS Styling

This handles the visual look, hover/active states, and hides panels by default (except our first one):

/* Core accordion button style */
.accordion {
  background-color: #eee;
  color: #444;
  cursor: pointer;
  padding: 18px;
  width: 100%;
  text-align: left;
  border: none;
  outline: none;
  transition: 0.4s;
}

/* Active/hover state for buttons */
.active, .accordion:hover {
  background-color: #ccc;
}

/* Panel styling - hidden by default */
.panel {
  padding: 0 18px;
  background-color: white;
  display: none;
  overflow: hidden;
}

/* Plus/minus indicators for better UX */
.accordion:after {
  content: '\02795'; /* Plus sign */
  font-size: 13px;
  color: #777;
  float: right;
  margin-left: 5px;
}

.active:after {
  content: "\2796"; /* Minus sign */
}

JavaScript Functionality

This adds the toggle behavior when users click the buttons:

// Grab all accordion buttons
const acc = document.getElementsByClassName("accordion");

// Loop through each button to attach click listeners
for (let i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function() {
    // Toggle the active class on the clicked button
    this.classList.toggle("active");
    
    // Get the panel directly after the button
    const panel = this.nextElementSibling;
    
    // Switch the panel's display state
    if (panel.style.display === "block") {
      panel.style.display = "none";
    } else {
      panel.style.display = "block";
    }
  });
}

Quick Notes

  • The first panel stays open on page load because we explicitly set display: block in the HTML and gave its button the active class.
  • The transition in CSS makes the button color change smooth, and the plus/minus icons make the state obvious to users.
  • The JS works for any number of accordion sections—just copy the button/panel pair to add more.

内容的提问来源于stack exchange,提问作者Marius Trandafir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 13:02:36