示例工程 33-dialog-popover
33-dialog-popover/demo-01-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>对话框演示</title>
<style>
body { margin: 0; padding: 32px; background: #f8fafc; color: #0f172a; font-family: system-ui, sans-serif; line-height: 1.7; }
.box { max-width: 720px; margin-bottom: 16px; padding: 18px; border: 1px solid #e2e8f0; border-radius: 12px; background: #fff; }
h1 { margin: 0 0 16px; font-size: 28px; }
h2 { margin: 0 0 8px; font-size: 17px; }
p { margin: 0 0 10px; color: #334155; }
button { padding: 7px 14px; border: 1px solid #2563eb; border-radius: 6px; background: #2563eb; color: #fff; font: inherit; }
button.secondary { background: #fff; color: #334155; border-color: #cbd5e1; }
dialog { width: min(92vw, 400px); padding: 18px; border: 1px solid #cbd5e1; border-radius: 10px; }
dialog::backdrop { background: rgb(15 23 42 / 0.45); }
pre { margin: 8px 0 0; padding: 12px; overflow-x: auto; border-radius: 8px; background: #0f172a; color: #e2e8f0; font-size: 12px; }
</style>
</head>
<body>
<h1>对话框</h1>
<div class="box">
<h2>打开模态</h2>
<button type="button" commandfor="demo-dialog" command="show-modal">打开对话框</button>
<p>支持 commandfor 的浏览器会直接打开模态;不支持的浏览器可以用脚本 showModal()。</p>
<dialog id="demo-dialog" aria-labelledby="demo-title" closedby="any">
<h2 id="demo-title">保存修改?</h2>
<p>保存后旧版本会被覆盖。</p>
<form method="dialog">
<button type="submit" class="secondary" autofocus>取消</button>
<button type="submit" value="save">保存</button>
</form>
</dialog>
</div>
<div class="box">
<h2>API</h2>
<pre>dialog.showModal();
dialog.close("save");
// close / cancel 事件
// :modal 伪类</pre>
</div>
</body>
</html>