示例工程 21-web-components
21-web-components/03-light-dom/index.html
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>21 · 无 Shadow DOM 的计数器</title>
<style>
x-light-counter { display: inline-block; border: 1px solid #999; padding: 8px; }
x-light-counter button { min-width: 32px; }
</style>
</head>
<body>
<x-light-counter></x-light-counter>
<script type="module">
import './x-light-counter.js';
import { run } from './main.js';
const el = document.querySelector('x-light-counter');
el.addEventListener('count-change', (event) => console.log('count-change =>', event.detail.count));
run();
</script>
</body>
</html>