html Boostrap 5 -单击特定元素时无法使用stopPropagation()禁用折叠

mrzz3bfm  于 2022-12-16  发布在  其他
关注(0)|答案(1)|浏览(113)

我尝试在单击元素中的复选框时禁用该元素的折叠/展开。
对于我在codepen上的示例,我的代码使用Boostrap v4,但使用v5时,stopPropagation函数似乎被忽略了。
请参见此处-https://codepen.io/s89_/pen/oNyKbYE
我***可以***将单击功能 Package 到一个div中,该div不 Package 输入复选框,但我希望整个行宽都是可单击的,这样禁用事件传播似乎更容易。
HTML:-

<div id="collapse-example">
  <div class="card col-6" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
    <div class="card-header d-flex justify-content-between" id="headingOne">
      <h5 class="mb-0">
        <button class="btn btn-link">
          Collapsible Group Item #1
        </button>
      </h5>
      <div class="checkboxes">
        <input type="checkbox" id="custom-input">
      </div>
    </div>

    <div id="collapseOne" class="collapse" aria-labelledby="headingOne" data-parent="#accordion">
      <div class="card-body">
        Loreum ipsum
      </div>
    </div>
  </div>
</div>

JS系统

const input = document.getElementById('custom-input')
    input.addEventListener('click', function(event) {
      event.stopPropagation();
});
jrcvhitl

jrcvhitl1#

将data-bs-toggle=“折叠”添加到复选框

<input type="checkbox" id="custom-input" data-bs-toggle="collapse">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div id="collapse-example">
  <div class="card col-6" data-bs-target="#collapseOne" data-bs-toggle="collapse"  aria-expanded="true" aria-controls="collapseOne">
    <div class="card-header d-flex justify-content-between" id="headingOne">
      <h5 class="mb-0">
        <button class="btn btn-link">
          Collapsible Group Item #1
        </button>
      </h5>
      <div class="checkboxes">
        <input type="checkbox" id="custom-input" data-bs-toggle="collapse" >
      </div>
    </div>

    <div id="collapseOne" class="collapse" aria-labelledby="headingOne" data-parent="#accordion">
      <div class="card-body">
        Loreum ipsum
      </div>
    </div>
  </div>
</div>

相关问题