cancel
event fires on a
<dialog>
when the user instructs the browser that they wish to dismiss the current open dialog. For example, the browser might fire this event when the user presses the
Esc
key or clicks a "Close dialog" button which is part of the browser's UI.
| 冒泡 | No |
|---|---|
| 可取消 | Yes |
| 接口 |
事件
|
| Event handler |
oncancel
|
<dialog class="example-dialog">
<button class="close" type="reset">Close</button>
</dialog>
<button class="open-dialog">Open dialog</button>
<div class="result"></div>
button, div {
margin: .5rem;
}
const result = document.querySelector('.result');
const dialog = document.querySelector('.example-dialog');
dialog.addEventListener('cancel', (event) => {
result.textContent = 'dialog was canceled';
});
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();
});
| 规范 | 状态 |
|---|---|
|
HTML 实时标准
The definition of 'cancel' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
cancel
event
|
Chrome Yes | Edge 79 | Firefox 78 | IE No | Opera ? | Safari No | WebView Android No | Chrome Android No | Firefox Android No | Opera Android No | Safari iOS No | Samsung Internet Android No |
完整支持
不支持
兼容性未知
GlobalEventHandlers.oncancel
<dialog>
element
close
HTMLDialogElement
cancel
close