var attention = function (e) {
var confirmationMessage = "修改尚未保存!";
console.log(e.);
(e || window.event).returnValue = confirmationMessage; // Gecko and Trident
return confirmationMessage; // Gecko and WebKit
};
window.addEventListener('beforeunload', attention);
绑定到 window 上时起效,绑定到其他页面元素则不行,例如
var input = document.querySelector('table>input')
input.addEventListener('beforeunload', attention)
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
http://guoyankun.iteye.com/blog/1765858看看这个吧,能用在三个window,body,frameset这三个元素上。
Which HTML tags support the onbeforeunload event?
官方文档对 beforeunload 事件的说明
官方说,只在 window 对象上支持这个事件
但是我在国外的 w3school 上看到说 body 元素也支持这个事件,然后我就在自己浏览器上试了一下,然而没什么卵用(浏览器:Chrome 53.0)。还是官网的对,只在window 对象上支持这个事件。