将下拉按钮与工具提示一起使用会导致 BS 5 中出现 JavaScript 错误

Using dropdown button together with tooltip causes a JavaScript error in BS 5

将下拉按钮与工具提示一起使用时会在 Bootstrap 5 中引发 JavaScript 错误。请 运行 此代码段:

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

    <div class="btn-group">
      <button id="btnFingerprints" type="button" class="btn btn-circle btn-secondary me-2 dropdown-toggle" data-bs-toggle-second="tooltip" data-bs-toggle="dropdown" aria-expanded="false" title="Testing">
          <i class="ti-exchange-vertical"></i>
      </button>

      <div class="dropdown-menu">
        <button class="dropdown-item" type="button" id="btnBajarHuellas">
            <i class="ti-download me-2"></i>Bajar huellas
        </button>
        
        <button class="dropdown-item" type="button" id="btnSubirHuellas">
            <i class="ti-upload me-2"></i>Subir huellas
        </button>
      </div>

      <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

<script>
let tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle-second="tooltip"]'))
    tooltipTriggerList.map(function (tooltipTriggerEl) {
        return new bootstrap.Tooltip(tooltipTriggerEl)
    })
</script>

这里需要考虑的几点:

我该怎么做?

谢谢

海梅

Bootstrap 不允许每个元素有多个实例,因此解决方案很简单,只需将按钮包装在包含工具提示的容器中即可:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
  integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<div class="btn-group">
  <div data-bs-toggle-second="tooltip" title="Testing">
    <button id="btnFingerprints" type="button" class="btn btn-circle btn-secondary me-2 dropdown-toggle"
      data-bs-toggle="dropdown" aria-expanded="false">
      <i class="ti-exchange-vertical"></i>
    </button>

    <div class="dropdown-menu">
      <button class="dropdown-item" type="button" id="btnBajarHuellas">
        <i class="ti-download me-2"></i>Bajar huellas
      </button>

      <button class="dropdown-item" type="button" id="btnSubirHuellas">
        <i class="ti-upload me-2"></i>Subir huellas
      </button>
    </div>
  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
  integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

<script>
  let tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle-second="tooltip"]'))
  tooltipTriggerList.map(function (tooltipTriggerEl) {
    return new bootstrap.Tooltip(tooltipTriggerEl)
  })
</script>