将下拉按钮与工具提示一起使用会导致 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>
这里需要考虑的几点:
- data-bs-toggle 已经用于允许按钮有一个下拉菜单,所以我创建了一个 data-bs-toggle-second 属性。
- 页面加载时不会创建此按钮。它是由刷新 DIV 的 ajax 调用创建的,所以我认为我应该手动实例化工具提示,因为数据属性不是标准的。对于 data-bs-toggle 属性,我不需要从 Bootstrap 调用任何东西,因为该属性是标准的。
我该怎么做?
谢谢
海梅
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>
将下拉按钮与工具提示一起使用时会在 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>
这里需要考虑的几点:
- data-bs-toggle 已经用于允许按钮有一个下拉菜单,所以我创建了一个 data-bs-toggle-second 属性。
- 页面加载时不会创建此按钮。它是由刷新 DIV 的 ajax 调用创建的,所以我认为我应该手动实例化工具提示,因为数据属性不是标准的。对于 data-bs-toggle 属性,我不需要从 Bootstrap 调用任何东西,因为该属性是标准的。
我该怎么做?
谢谢
海梅
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>