HTMLDialogElement: close event - Web APIs 编辑
The close
event is fired on an HTMLDialogElement
object when the dialog it represents has been closed.
Bubbles | No |
---|---|
Cancelable | No |
Interface | Event |
Event handler property | onclose |
Examples
Live example
HTML
<dialog class="example-dialog">
<button class="close" type="reset">Close</button>
</dialog>
<button class="open-dialog">Open dialog</button>
<div class="result"></div>
CSS
button, div {
margin: .5rem;
}
JS
const result = document.querySelector('.result');
const dialog = document.querySelector('.example-dialog');
dialog.addEventListener('close', (event) => {
result.textContent = 'dialog was closed';
});
const openDialog = document.querySelector('.open-dialog');
openDialog.addEventListener('click', () => {
if (typeof dialog.showModal === 'function') {
dialog.showModal();
result.textContent = '';
} else {
result.textContent = 'The dialog API is not supported by this browser';
}
});
const closeButton = document.querySelector('.close');
closeButton.addEventListener('click', () => {
dialog.close();
});
Result
Specifications
Specification | Status |
---|---|
HTML Living Standard The definition of 'close' in that specification. | Living Standard |
Browser compatibility
BCD tables only load in the browser
See also
- HTML
<dialog>
element
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论