示例工程 28-buttons-dialog
28-buttons-dialog/demo-02-dialog-popover/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>对话框与 popover 演示</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 { margin: 0 8px 8px 0; 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, 380px); padding: 18px; border: 1px solid #cbd5e1; border-radius: 10px; }
dialog::backdrop { background: rgb(15 23 42 / 0.45); }
[popover] { width: 220px; padding: 10px; border: 1px solid #cbd5e1; border-radius: 8px; background: #fff; }
[popover] ul { margin: 0; padding-left: 18px; }
code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, monospace; }
pre { margin: 8px 0 0; padding: 10px; overflow-x: auto; border-radius: 8px; background: #0f172a; color: #e2e8f0; font-size: 12px; }
</style>
</head>
<body>
<h1>对话框与 popover</h1>
<div class="box">
<h2>dialog</h2>
<dialog open aria-labelledby="dlg-title">
<h2 id="dlg-title">保存修改?</h2>
<p>保存后旧版本会被覆盖。</p>
<form method="dialog">
<button type="submit" class="secondary">取消</button>
<button type="submit" value="save">保存</button>
</form>
</dialog>
<pre>dialog.showModal(); // 模态
dialog.close(); // 关闭</pre>
</div>
<div class="box">
<h2>popover</h2>
<button type="button" popovertarget="menu">打开菜单</button>
<div id="menu" popover>
<ul>
<li><a href="#top">项目一</a></li>
<li><a href="#top">项目二</a></li>
</ul>
</div>
<p>点击按钮打开,点击外部或 Esc 关闭。</p>
</div>
</body>
</html>