close event is fired on an HTMLDialogElement object when the dialog it represents has been closed.

冒泡 No
可取消 No
接口 事件
事件处理程序特性 onclose

范例

实时范例

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();
});
					

结果

规范

规范 状态
HTML 实时标准
The definition of 'close' in that specification.
实时标准

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
close event Chrome Yes Edge 79 Firefox No IE No Opera ? Safari No WebView Android No Chrome Android No Firefox Android No Opera Android No Safari iOS No Samsung Internet Android No

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

另请参阅

元数据

  • 最后修改: