如何捕获按下 ESC 键以关闭 Bootstrap 模态框
How to capture the pressing of the ESC key to close a Bootstrap Modal
我有一个 bootstrap 模式,我用它来更改缩略图的大小。我在 bootstrap 模式上有一个保存和取消按钮。如果我点击取消按钮,缩略图将恢复到之前的大小。我希望通过按 ESC 键或在模式外单击来实现相同的行为。但是,我一直无法捕捉到这些事件。
在bootstrap3中可以使用
$('#my-modal').on('hidden.bs.modal', function () {
window.alert('event fired!');
});
对于Bootstrap5,语法是
const modalElem = document.getElementById('modaldlg');
const myModal = new bootstrap.Modal(modalElem);
modalElem.addEventListener('hide.bs.modal', (event) => {
window.alert('event fired!');
});
我有一个 bootstrap 模式,我用它来更改缩略图的大小。我在 bootstrap 模式上有一个保存和取消按钮。如果我点击取消按钮,缩略图将恢复到之前的大小。我希望通过按 ESC 键或在模式外单击来实现相同的行为。但是,我一直无法捕捉到这些事件。
在bootstrap3中可以使用
$('#my-modal').on('hidden.bs.modal', function () {
window.alert('event fired!');
});
对于Bootstrap5,语法是
const modalElem = document.getElementById('modaldlg');
const myModal = new bootstrap.Modal(modalElem);
modalElem.addEventListener('hide.bs.modal', (event) => {
window.alert('event fired!');
});