示例工程 43-shadow-dom

43-shadow-dom/demo-02-events/index.html

html · 3.0 KB

运行:事件与重定向演示

点击「运行示例」后,左侧显示源码、右侧显示运行结果;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; }
      x-ping { display: block; margin: 8px 0; }
      ol { margin: 8px 0 0; padding-left: 22px; color: #334155; font-size: 13px; }
      code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, monospace; }
    </style>
  </head>
  <body>
    <h1>事件与重定向</h1>

    <div class="box">
      <h2>两个按钮</h2>
      <p>「发送 composed」会穿过 Shadow 边界;「发送本地」只在内部冒泡。</p>
      <x-ping></x-ping>
      <ol id="log" aria-live="polite"></ol>
    </div>

    <div class="box">
      <h2>为什么 target 变了</h2>
      <p>外部监听看到的是宿主 <code>X-PING</code>,真实来源在 <code>composedPath()</code> 里。</p>
    </div>

    <script>
      class XPing extends HTMLElement {
        constructor() {
          super();
          const root = this.attachShadow({ mode: "open" });
          root.innerHTML = `
            <style>
              :host { display: inline-flex; gap: 8px; padding: 8px; border: 1px solid #e2e8f0; border-radius: 10px; background: #fff; }
              button { padding: 5px 10px; border: 1px solid #2563eb; border-radius: 6px; background: #2563eb; color: #fff; font: inherit; font-size: 13px; }
              button + button { border-color: #cbd5e1; background: #fff; color: #0f172a; }
            </style>
            <button type="button" data-kind="composed">发送 composed</button>
            <button type="button" data-kind="local">发送本地</button>
          `;
          root.querySelectorAll("button").forEach((button) => {
            button.addEventListener("click", () => {
              const composed = button.dataset.kind === "composed";
              this.dispatchEvent(new CustomEvent("x-ping", {
                detail: { kind: button.dataset.kind },
                bubbles: true,
                composed,
              }));
            });
          });
        }
      }
      customElements.define("x-ping", XPing);

      const log = document.getElementById("log");
      const add = (text) => {
        const li = document.createElement("li");
        li.textContent = text;
        log.append(li);
      };
      document.querySelector("x-ping").addEventListener("x-ping", (event) => {
        add("外部收到 " + event.detail.kind + ",target=" + event.target.tagName);
        add("路径:" + event.composedPath().map((node) => node.nodeName ?? node).join(" → "));
      });
    </script>
  </body>
</html>