示例工程 31-modals-dropdowns
31-modals-dropdowns/demo-02-dialog/index.html
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>dialog 模态</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<h1>dialog 模态</h1>
<p>打开后背景不可交互;Esc 关闭,焦点回到按钮。</p>
<button id="open" class="btn" type="button">打开对话框</button>
<dialog id="dlg" class="dialog" aria-labelledby="t">
<form method="dialog" class="head">
<h2 id="t">删除这条记录?</h2>
<button class="close" type="submit" aria-label="关闭">×</button>
</form>
<p class="body">删除后无法恢复。</p>
<div class="foot">
<form method="dialog"><button class="btn" type="submit">取消</button></form>
<form method="dialog"><button class="btn danger" type="submit">删除</button></form>
</div>
</dialog>
<script>
const dlg = document.querySelector("#dlg");
document.querySelector("#open").addEventListener("click", () => dlg.showModal());
</script>
</body>
</html>