-
Notifications
You must be signed in to change notification settings - Fork 0
/
shadow-dom.html
48 lines (43 loc) · 1.4 KB
/
shadow-dom.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Shadow DOM Example</title>
</head>
<body>
<custom-element></custom-element>
<script>
class CustomElement extends HTMLElement {
constructor() {
super();
// 创建一个 shadow root
const shadow = this.attachShadow({ mode: "open" });
// 创建内部结构
const wrapper = document.createElement("div");
wrapper.setAttribute("class", "wrapper");
const info = document.createElement("span");
info.setAttribute("class", "info");
info.textContent = "This is a shadow DOM component";
// 添加样式
const style = document.createElement("style");
style.textContent = `
.wrapper {
padding: 10px;
background-color: #f3f3f3;
}
.info {
color: blue;
}
`;
// 将元素和样式添加到 shadow DOM 中
shadow.appendChild(style);
shadow.appendChild(wrapper);
wrapper.appendChild(info);
}
}
// 定义自定义元素
customElements.define("custom-element", CustomElement);
</script>
</body>
</html>