Javascript HTMLDialogElement.showModal()的对立面是什么?

Javascript HTMLDialogElement.showModal()的对立面是什么?,javascript,dialog,html-5.2,Javascript,Dialog,Html 5.2,使用HTML5.2原生元素,我可以使用Javascript打开对话框 document.addEventListener('DOMContentLoaded', () => { const dialog = document.getElementsByTagName('dialog')[0]; dialog.showModal(); }); 如何使用Javascript关闭对话框?可以使用open()和close()方法打开和关闭对话框。使用这些方法的缺点是CSS伪元素::bac

使用HTML5.2原生元素,我可以使用Javascript打开对话框

document.addEventListener('DOMContentLoaded', () => {
  const dialog = document.getElementsByTagName('dialog')[0];
  dialog.showModal();
});

如何使用Javascript关闭对话框?

可以使用
open()
close()
方法打开和关闭对话框。使用这些方法的缺点是CSS伪元素
::background
不起作用

<button id="open">Open Dialog</button>
<button id="close">Close Dialog</button>

<dialog id="dialog">
  <h2>Dialog Title</h2>
  <p>Dialog content and other stuff will go here</p>
</dialog>

<script>
const dialog = document.getElementById("dialog");

document.getElementById("open").addEventListener("click", () => {
  dialog.show();
});

document.getElementById("close").addEventListener("click", () => {
  dialog.close();
});
</script>
打开对话框
关闭对话框
对话标题
对话框内容和其他内容将转到此处

const dialog=document.getElementById(“dialog”); document.getElementById(“打开”).addEventListener(“单击”,()=>{ dialog.show(); }); document.getElementById(“close”).addEventListener(“单击”,()=>{ dialog.close(); });
建议
.close()
-阅读它,看看我是否正确页面上的示例清楚地显示了使用
.close()
来关闭它。问题是什么?使用这些方法的缺点是CSS伪元素::background不起作用。