javascript - 如何制作自己的自定义模态事件
问题描述
我正在制作自己的模态。但是我需要像引导程序中的模态事件一样为我的模态设置事件。因为我希望在我自己的模态显示给用户之后发生一些事情
我的问题是这些引导模式事件相当于普通的香草js
show.bs.modal
显示.bs.modal
hide.bs.modal
hidden.bs.modal
解决方案
您可以为此使用JavaScript 事件。希望下面的例子有所帮助。
当show_modal
函数调用时,它将触发modal_show
事件。
当hide_modal
函数调用时,它将触发modal_hide
事件。
我们可以在任何地方收听这两个事件window.addEventListener('modal_show'...
function show_modal() {
// show modal logic here
// Create a custom event
const event = new Event('modal_show');
// Dispatch the event
window.dispatchEvent(event);
}
function hide_modal() {
// hide modal logic here
// Create a custom event
const event = new Event('modal_hide');
// Dispatch the event
window.dispatchEvent(event);
}
// Listen for the event.
window.addEventListener('modal_show', function (e) { /* ... */ }, false);
window.addEventListener('modal_hide', function (e) { /* ... */ }, false);
推荐阅读
- pandas - 从另一个数据帧的值中获取最大值
- javascript - DataTables 的格式正确的表抛出错误
- c# - 是否可以让多个类(无论是 UITableViewCell 还是 UIViewController)在 Xamarin.iOS 中共享一个 xib?
- azure - 将企业机器人模板(SDK V4+C#)从 Visual Studio 2017 发布到 azure 应用服务时出现问题
- wpf - WPF 和 EF 与等待异步,冻结我的 UI
- pandas - 读取具有列表数据类型的列
- sql - TSQL - 如何确定给定周数的一周的第一天和最后一天
- jira - 想知道为什么带有页面模块的 Jira Cloud 插件无法安装“project_type”条件集?
- amazon-web-services - 如何在 Node 中签署 S3 URL 时修复“SignatureDoesNotMatch”错误?
- java - 试图反转嵌套 for 循环的输出