引导程序手动折叠

时间:2020-08-28 10:05:38

标签: javascript html twitter-bootstrap

我有几个可折叠项,当可折叠div打开时,当用户单击另一个可打开另一个可折叠div但上一个div不能关闭的项目时,我面临一个问题。当用户单击另一个可打开另一个可折叠div的项目时,如何关闭打开的可折叠div。

我尝试了以下类似的方法

<li><a class="layer-item" data-toggle="collapse" href="#baseLayer" role="button" aria-expanded="false" aria-controls="baseLayer" onclick="selectLayer('base')" id="base-layer-btn" title="Base layer">Base layer</a></li>
<li><a class="layer-item" data-toggle="collapse" href="#soilLayer" role="button" aria-expanded="false" aria-controls="soilLayer" onclick="selectLayer('soil')" id="soil-layer-btn" title="Soil layers">Soil layers</a></li>

<div class="collapse" id="baseLayer">...</div>
<div class="collapse" id="soilLayer">...</div>

在我的脚本标签内

<script>
  function selectLayer(layer){
    if(layer == 'soil'){
      $('#baseLayer').collapse('hide')
    }else if(layer == 'base'){
      $('#soilLayer').collapse('hide')
    }
  }
</script>

但是此解决方案无法按预期正常运行。实现我正在寻求的可折叠行为的最佳方法是什么?

2 个答案:

答案 0 :(得分:0)

请尝试这个

function selectLayer(layer){
    if(layer == 'soil'){
      $('#soilLayer').toggleClass('show')
    }else if(layer == 'base'){
      $('#baseLayer').toggleClass('show')
    }
  }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<li><a class="layer-item" href="#baseLayer" role="button" aria-expanded="false" aria-controls="baseLayer" onclick="selectLayer('base')" id="base-layer-btn" title="Base layer">Base layer</a></li>
<li><a class="layer-item"  href="#soilLayer" role="button" aria-expanded="false" aria-controls="soilLayer" onclick="selectLayer('soil')" id="soil-layer-btn" title="Soil layers">Soil layers</a></li>

<div class="collapse" id="baseLayer">Base</div>
<div class="collapse" id="soilLayer">soil</div>

答案 1 :(得分:0)

简单解决方案

添加一个自定义jquery方法即可解决!

/* On click of any of your list items, only the selected one will have the class of 
`show` and all other will automatically get the class of `hide` */

 $('.layer-item').click(() => {
    $('.collapse').collapse('hide');
  });
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous">
</script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous">
</script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous">
</script>

<ul>
  <li><a class="layer-item text-primary" data-toggle="collapse" href="#baseLayer" role="button" aria-expanded="false" aria-controls="baseLayer" id="base-layer-btn" title="Base layer">Base layer</a></li>
  <li><a class="layer-item text-warning" data-toggle="collapse" href="#soilLayer" role="button" aria-expanded="false" aria-controls="soilLayer" id="soil-layer-btn" title="Soil layers">Soil layers</a></li>
</ul>


<div class="collapse" id="baseLayer">
  <h1 class="bg-primary">Base Layer</h1>
</div>
<div class="collapse" id="soilLayer">
  <h1 class="bg-warning">Soil Layer</h1>
</div>

您可能希望在jsfiddle上看到它的作用