javascript - 在用户确认之前如何防止页面卸载?
问题描述
我有弹出窗口出现,但页面继续卸载。.js-close
在单击或提交弹出窗口中的表单之前,如何防止页面卸载?
$(document).ready(() => {
window.onbeforeunload = () => {
const newsSubscribePopup = $('#news-subscribe-popup');
if (newsSubscribePopup.length) {
$.magnificPopup.open({
items: {
src: newsSubscribePopup,
},
type: 'inline',
preloader: true,
modal: true,
showCloseBtn: false,
});
}
};
});`
解决方案
您可以将 confirm() 函数与 onclick 处理程序一起使用。
下面是一个简单的例子:
<div class="js-close" data-confirm="Are you sure to delete this item?">action</div>
$('.js-close').on( "click", function() {
var choice = confirm($(this).attr("data-confirm"));
if (choice) {
alert('ok');
}else{
alert('no');
}
});
推荐阅读
- c# - ResourceDictionary 中所有 Windows 的 C# WPF 背景样式 - 具有相对路径的目标类型?
- flyway - how to set the flyway community version to pro version
- java - 如何删除 IntelliJ IDEA 中未使用的代码?
- codeigniter - 插入一次两次表用户循环出错
- angular - Angular 7 在子路由的 CanActivate 之前解析数据
- where-clause - 基于参数输入的 SQL 案例 When within where 子句
- javascript - 防止反应表中不必要的重绘
- django - 如何重用现有的管理模型来显示自定义查询集?
- html - 如何在带有网格的引导程序中设置中间和底部文本
- python - Python pandas空df但列有元素