添加新块后为什么崩溃/消耗不起作用?

时间:2017-05-11 12:03:34

标签: javascript html twitter-bootstrap

为什么我的js代码不起作用?之前有用,但是当我添加下一个块时它不起作用:

<div class="collapse" id="collapse-block">

我有崩溃块(id='collapse-block'),里面有3个其他崩溃块。这个js代码适用于按钮(id='expand-collapse'),它将展开/折叠 div中的块(id='blocks'

&#13;
&#13;
$(function() {
  $('#expand-collapse').on('click', function() {
    var allCollapsed = true;
    $('#blocks .collapse').each(function(i, block) {
      if ($(block).hasClass('show')) {
        allCollapsed = false;
      }
    });
    $('#blocks .collapse').each(function(i, block) {
      if (allCollapsed) {
        $(block).collapse('show');
      } else {
        $(block).collapse('hide');
      }
    });
  });
});
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">

<div class="card">
  <div class="card-header">
    <button class="btn btn-secondary" type="button" data-toggle="collapse" data-target="#collapse-block" aria-expanded="false" aria-controls="collapse-block">OPEN/CLOSE</button>

    <button id='expand-collapse' type="button">OPEN/CLOSE BLOCKS</button>
  </div>

  <div class="card-block">
    <div class="collapse" id="collapse-block">
      <div id="blocks">
        <div class="list-group">

          <div class="list-group-item">
            <a data-toggle="collapse" href="#block-1" aria-expanded="false" aria-controls="block-1">FIRST BLOCK</a>
            <div class="collapse block" id="block-1">
               FIRST BLOCK
            </div>
          </div>

          <div class="list-group-item">
            <a data-toggle="collapse" href="#block-2" aria-expanded="false" aria-controls="block-2">SECOND BLOCK</a>
            <div class="collapse block" id="block-2">
              SECOND BLOCK
            </div>
          </div>

          <div class="list-group-item">
            <a data-toggle="collapse" href="#block-3" aria-expanded="false" aria-controls="block-3">THIRD BLOCK</a>
            <div class="collapse block" id="block-3">
              THIRD BLOCK
            </div>
          </div>

        </div>
      </div>
    </div>
  </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:1)

根据javascript标准,每当您修改DOM,添加或删除子项时,事件都会被删除,您将不得不再次添加事件。这是问题。我猜。

修改

修改innerHTML会导致重新解析内容并重新创建DOM节点,从而丢失您附加的处理程序。像第一个示例中那样添加元素不会导致该行为,因此不必重新解析,因为您明确地修改了DOM树。

处理此问题的另一个好方法是使用insertAdjacentHTML()。例如:

document.body.insertAdjacentHTML('beforeend', '<br>')

答案 1 :(得分:1)

您需要包含对Bootstrap脚本或Collapse.js

的引用
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

答案 2 :(得分:1)

您的代码中没有添加bootstrap css,我认为这就是问题

一样引用它
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/bootstrap/3/css/bootstrap.css" />

工作小提琴https://jsfiddle.net/qcdk68et/

答案 3 :(得分:0)

SORRY!这都怪我!我只是忘了在html中添加js代码文件。问题已经结束。