如何使用jQuery创建手风琴?

时间:2018-11-25 11:45:04

标签: javascript jquery html twitter-bootstrap

我正在尝试创建一个手风琴,其中默认情况下默认情况下一个步骤处于打开状态,其余步骤可以通过单击编辑按钮来打开。下面定义的结构。

Step 1

Hello world. Lorem ipsum.
--------------------------------------------
Step 2                                  Edit
--------------------------------------------
Step 3                                  Edit
--------------------------------------------
Step 4                                  Edit

现在,如您所见,默认情况下会显示第1步的内容。因此,如果我单击步骤2的“编辑”按钮,则应该折叠步骤1,并且应该可以看到步骤2的内容。

这是我到目前为止提出的内容。

HTML:

<h6 class="ml-3 mt-3 step-1"><a id="step-one" href="#">Step 1</a></h6>
<br style="clear:both;">
<div id="promo-code" class="mt-5 ml-3">
<p>Abc content</p>
<button type="button" class="btn btn-primary">Continue</button>
</div>

<hr class="ml-3">

<div class="mt-5 ml-3" id="promo-box">
<h6>Step 2</h6>
<p>Abc content</p>
<button type="button" class="btn btn-primary">Continue</button>
</div>

JS:

$("#promo-code").click(function(){
document.getElementById("promo-code").style.display = "none";
document.getElementById("promo-box").style.display = "block";
});

$("#step-one").click(function(){
document.getElementById("promo-code").style.display = "block";
document.getElementById("promo-box").style.display = "none";
});

CSS:

#promo-code {
font-size: 15px;
color: #02b875;
}

#promo-box {
display: none;
}

.step-1 {
margin-bottom: -30px;
}

1 个答案:

答案 0 :(得分:2)

由于您正在使用引导程序,因此这里是引导程序示例。运行此代码片段,功能几乎与您想要实现的功能相同,请根据需要进行修改。

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.min.js"></script>

<div class="accordion" id="accordionExample">
  <div class="card">
    <div class="card-header" id="headingOne">
      <h5 class="mb-0"> Step 1
        <button class="btn btn-link float-right" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">Edit</button>
      </h5>
    </div>

    <div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordionExample">
      <div class="card-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
      </div>
    </div>
  </div>
  <div class="card">
    <div class="card-header" id="headingTwo">
      <h5 class="mb-0"> Step 2
        <button class="btn btn-link collapsed float-right" type="button" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">Edit</button>
      </h5>
    </div>
    <div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample">
      <div class="card-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
      </div>
    </div>
  </div>
  <div class="card">
    <div class="card-header" id="headingThree">
      <h5 class="mb-0">Step 3
        <button class="btn btn-link collapsed float-right" type="button" data-toggle="collapse" data-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
          Edit
        </button>
      </h5>
    </div>
    <div id="collapseThree" class="collapse" aria-labelledby="headingThree" data-parent="#accordionExample">
      <div class="card-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
      </div>
    </div>
  </div>
</div>