手风琴在Bootstrap 4折叠中不起作用

时间:2017-02-23 10:35:29

标签: javascript jquery twitter-bootstrap

https://fiddle.jshell.net/8v72rtxb/
我有以下代码。

  <div class="menu-horizontal container">
    <div class="row menu-container" id="menu">
      <div class="col-md-3 menu-item">
        <a data-toggle="collapse" class="collapsed collapse-toggle" data-parent="#menu" href="#studies-collapse" aria-expanded="false" aria-controls="collapseExample">
          Studies
        </a>
        <div class="collapse" id="studies-collapse">
          <div class="container">
            <div class="row">
              Something...
            </div>
          </div>
        </div>
      </div>

      <div class="col-md-3 menu-item">
        <a data-toggle="collapse" class="collapsed collapse-toggle" data-parent="#menu" href="#research-collapse" aria-expanded="false" aria-controls="collapseExample">
          Research
        </a>
        <div class="collapse" id="research-collapse">
          <div class="container">
            <div class="row">
              Something...
            </div>
          </div>
        </div>
      </div>

    </div>
  </div>

我不知道为什么data-parent不起作用。展开一个崩溃div不会关闭另一个崩溃。我试图从bootstrap的例子中复制代码,但它仍然无效。 同样在我的项目中,我在jquery.js之前添加了bootstrap.min.js。我已经检查了一些其他问题,他们说bootstrap.min.js可能已被包括两次。但那应该是这样吗?
谢谢你的阅读!

4 个答案:

答案 0 :(得分:5)

我想到了解决方法。即使不使用data-parent属性也是如此。 https://fiddle.jshell.net/zyd1vL4o/

$(document).ready(function(){
    $('.collapse').on('show.bs.collapse', function (e) {
        $('.collapse').collapse("hide")
    })
})

答案 1 :(得分:0)

完成此jsfiddle

这是html代码,

<div id="accordion" role="tablist" aria-multiselectable="true">
  <div class="card">
    <div class="card-header" role="tab" id="headingOne">
      <h5 class="mb-0">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
          Studies
        </a>
      </h5>
    </div>

    <div id="collapseOne" class="collapse " role="tabpanel" aria-labelledby="headingOne">
      <div class="card-block">
        <div id="studies-collapse">
          <div class="container">
            <div class="row">
              <div class="col-md-3">
                <a href="" class="menu-subitem">
                    Study Guides
                </a>
                <a href="" class="menu-subitem">
                    Counselling and Instructions
                </a>
                <a href="" class="menu-subitem">
                    Student Laboratory
                </a>
                <a href="" class="menu-subitem">
                    Student Exchange
                </a>
              </div>
              <div class="col-md-3">
                <a href="" class="menu-subitem">
                    Post‐graduate studies
                </a>
                <a href="" class="menu-subitem">
                    Theses
                </a>
                <a href="" class="menu-subitem">
                    Exams
                </a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="card">
    <div class="card-header" role="tab" id="headingTwo">
      <h5 class="mb-0">
        <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
          Research
        </a>
      </h5>
    </div>
    <div id="collapseTwo" class="collapse" role="tabpanel" aria-labelledby="headingTwo">
      <div class="card-block">
        <div  id="research-collapse">
          <div class="container">
            <div class="row">
              <div class="col-md-3">
                <a href="" class="menu-subitem">
                    Study Guides
                </a>
                <a href="" class="menu-subitem">
                    Counselling and Instructions
                </a>
                <a href="" class="menu-subitem">
                    Student Laboratory
                </a>
                <a href="" class="menu-subitem">
                    Student Exchange
                </a>
              </div>
              <div class="col-md-3">
                <a href="" class="menu-subitem">
                    Post‐graduate studies
                </a>
                <a href="" class="menu-subitem">
                    Theses
                </a>
                <a href="" class="menu-subitem">
                    Exams
                </a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

答案 2 :(得分:0)

使用:

&#13;
&#13;
<div class="menu-horizontal container">
    <div class="row menu-container" id="menu">
        <div class="col-md-3 menu-item">
            <a data-toggle="collapse" class="collapsed" data-target="#studies-collapse" href="#studies-collapse" aria-expanded="false" aria-controls="studies-collapse">
                Studies
            </a>
            <div class="collapse" id="studies-collapse" data-parent="#menu">
                <div class="container">
                    <div class="row">
                        Something...
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3 menu-item">
            <a data-toggle="collapse" class="collapsed" data-target="#research-collapse" href="#research-collapse" aria-expanded="false" aria-controls="research-collapse">
                Research
            </a>
            <div class="collapse" id="research-collapse" data-parent="#menu">
                <div class="container">
                    <div class="row">
                        Something...
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

答案 3 :(得分:-1)

如果它没有关闭,那么你可能没有正确链接到你的bootstrap js。你不应该添加任何额外的东西。 检查以下js文件:

  • bootstrap.min.js
  • bootstrap.bundle.min.js

如果你要创建一个bootstrap主题,那么在函数中执行以下操作:
  wp_register_script( 'write your theme name here', get_template_directory_uri() . '/bootstrap/js/bootstrap.min.js', array( 'jquery' ) );