javascript - beforeunload 事件可以绑定到什么元素上面?
伊谢尔伦
伊谢尔伦 2017-04-10 16:56:58
[JavaScript讨论组]
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)
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(2)
阿神

http://guoyankun.iteye.com/blog/1765858看看这个吧,能用在三个window,body,frameset这三个元素上。

PHPz

Which HTML tags support the onbeforeunload event?

官方文档对 beforeunload 事件的说明

Let event be a new BeforeUnloadEvent event object with the name beforeunload, which does not bubble but is cancelable.
Dispatch: Dispatch event at the Document's Window object.

官方说,只在 window 对象上支持这个事件

但是我在国外的 w3school 上看到说 body 元素也支持这个事件,然后我就在自己浏览器上试了一下,然而没什么卵用(浏览器:Chrome 53.0)。还是官网的对,只在window 对象上支持这个事件

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号