我的 Bootstrap 5 模式和放置在其上的弹出窗口有问题。
在我的 HTML 中,我有以下内容,当用户将鼠标悬停在按钮上时,它会正确显示。弹出窗口显示“您确定吗?”键入提示并建议用户单击按钮(悬停)以继续。单击该按钮即可获取焦点并保持弹出窗口打开,直到再次单击或单击离开该元素。
这允许鼠标指针进入弹出窗口内容并单击“继续”按钮。
这就是我想要的东西:https://bootstrap-confirmation.js.org,但有一些有关按钮悬停时显示的功能的更多详细信息...
不管怎样,效果很好!第一次...如果模式被隐藏然后再次显示,则只有弹出窗口触发器的悬停部分起作用,并且单击按钮确实会触发它的侦听器,但弹出窗口拒绝保持打开状态(根据“焦点”触发器) )..
请帮忙,这让我发疯!
令人沮丧的是,下面的代码片段似乎确实有效,尽管在关闭并重新打开模式时会抛出错误,我不确定为什么?
编辑添加:Chrome 和 Safari 中均存在该问题,Safari 将创建控制台错误:
[Error] TypeError: undefined is not an object (evaluating 't.nodeType')
o (bootstrap.bundle.min.js:6:823)
_typeCheckConfig (bootstrap.bundle.min.js:6:7060)
_getConfig (bootstrap.bundle.min.js:6:69112)
W (bootstrap.bundle.min.js:6:7420)
cn (bootstrap.bundle.min.js:6:62612)
un
(anonymous function) (locate_dev.js:394)
dispatchEvent
trigger (bootstrap.bundle.min.js:6:5516)
(anonymous function) (bootstrap.bundle.min.js:6:52949)
a (bootstrap.bundle.min.js:6:2488)
dispatchEvent
s (bootstrap.bundle.min.js:6:736)
(anonymous function) (bootstrap.bundle.min.js:6:2539)
locate_dev.js 第 394 行的代码是弹出窗口初始化,可疑:
// 1 - Init the popover
var locateBtnClosePopover = new bootstrap.Popover($('#locate-btn-close'), {
html: true,
sanitize: false,
customClass: 'locate-footer-popover',
title: "Are you sure?",
content: $('[data-name="close-btn-popover-content"]')
});
let locateScreen = new bootstrap.Modal(document.getElementById('locateScreen'));
const locateModal = document.getElementById('locateScreen')
locateModal.addEventListener('shown.bs.modal', event => {
const locateBtnClosePopover = new bootstrap.Popover($('#locate-btn-close'), {
html: true,
sanitize: false,
customClass: 'locate-footer-popover',
title: "Are you sure?",
// trigger: 'focus',
content: $('[data-name="close-btn-popover-content"]')
});
// 2 - Listen for clicks on the 'Close' button
$('#locate-btn-close').off('click');
$('#locate-btn-close').on('click', function() {
// This is firing, but the popover is closing when the mouse moves towards
// the buttons, after the modal is shown once, hidden, then re-displayed.
// Show popover buttons in popover
$('[data-name="close-btn-popover-content"] .confirm-buttons').show();
// Assign listeners to the popover confirm buttons
//$('#locate-btn-close-confirm').off()
$('#locate-btn-close-confirm').on('click', function() { // Close Session screen
locateScreen.hide();
});
// Hide 'Click to continue' text
$('[data-name="close-btn-popover-content"] .input-group-sm .continue-text').hide()
});
// When the popover is initially shown, hide the buttons and make sure the text reads:
// 'click the button to continue', etc
$('#locate-btn-close').on('inserted.bs.popover', function() {
$('[data-name="close-btn-popover-content"] .confirm-buttons').hide()
$('[data-name="close-btn-popover-content"] .input-group-sm .continue-text').show()
});
});
locateModal.addEventListener('hidden.bs.modal', event => {
// At this point the $('[data-name="close-btn-popover-content"]') has been removed....
// Lets add it back in
$('#popover-close-btn-holder').html('You can choose to close or save this session at the next screen.
Click the button to continue...
');
});
似乎会产生错误,因为弹出窗口的内容在模式被隐藏和重新显示之间的某个时刻消失。
考虑:
var locateBtnClosePopover = new bootstrap.Popover($('#locate-btn-close'), {
html: true,
sanitize: false,
customClass: 'locate-footer-popover',
title: "Are you sure?",
content: $('[data-name="close-btn-popover-content"]')
});
测试 $('[data-name="close-btn-popover-content"]') 根本不返回任何内容。在初始化模式时(在初始化弹出窗口之前)手动将其附加到父 div 也没有帮助。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
解决了!
我从按钮 HTML 中删除了
data-bs-trigger="hover focus"属性并使用:locateBtnClosePopover = new bootstrap.Popover($('#locate-btn-close'), { html: true, sanitize: false, customClass: 'locate-footer-popover', title: "Are you sure?", trigger: 'hover focus', content: $('[data-name="close-btn-popover-content"]') });我还需要在
'hidden.bs.modal'上重建$('[data-name="close-btn-popover-content"]')中的内容,如下所示:locateModal.addEventListener('hidden.bs.modal', event => { // At this point the $('[data-name="close-btn-popover-content"]') has been removed.... // Lets add it back in $('#popover-close-btn-holder').html('
');
});You can choose to close or save this session at the next screen.
Click the button to continue...