当我通过Javascript添加“show”类时,折叠面板不滑动

mdfafbf1  于 2023-02-28  发布在  Java
关注(0)|答案(2)|浏览(91)

我在 accordion 工作,并打开一个面板上点击另一个链接<div id="open">Open It</div> .但它不能顺利打开.然而,当我打开点击卡头它的打开非常顺利.如何实现这一点?
超文本标记语言

<div id="accordion">
  <div class="card">
    <div class="card-header" id="headingOne">
      <h5 class="mb-0">
        <button class="btn btn-link" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
          Collapsible Group Item #1
        </button>
      </h5>
    </div>

    <div id="collapseOne" class="collapse" aria-labelledby="headingOne" data-parent="#accordion">
      <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 id="open">Open It</div>
</div>

函数

<script>
    $(document).on('click', '#open', function () {
        $('#collapseOne').toggleClass('show')
    })
</script>
3phpmpom

3phpmpom1#

您可以使用. slideToggle()

$(document).on('click', '#open', function () {
        $('#collapseOne').slideToggle()
    })
#collapseOne { display:none }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
I'm working on accordion, And Opening a panel on click on another link <div id="open">Open It</div>. but It's not opening smoothly. However when I open by click on card header it's opening very smoothly. How to achieve this?

html

<div id="accordion">
  <div class="card">
    <div class="card-header" id="headingOne">
      <h5 class="mb-0">
        <button class="btn btn-link" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
          Collapsible Group Item #1
        </button>
      </h5>
    </div>

    <div id="collapseOne" class="collapse" aria-labelledby="headingOne" data-parent="#accordion">
      <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 id="open">Open It</div>
</div>
aemubtdh

aemubtdh2#

从你的类/数据来看-看起来你在使用bootstrap 4 accordion。
在使用bootstrap等库时,谨慎的做法是使用所提供的API,在本例中:

$(document).on('click', '#open', function () {
    $('#collapseOne').collapse('toggle')
})

如果您只想显示而不想切换,还有其他选项,如.collapse("show")
更新了 Bootstrap :
x一个一个一个一个x一个一个二个x

相关问题