-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Expand file tree
/
Copy pathindex.md
More file actions
64 lines (45 loc) · 1.14 KB
/
index.md
File metadata and controls
64 lines (45 loc) · 1.14 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
---
title: Document:createTextNode() 方法
slug: Web/API/Document/createTextNode
l10n:
sourceCommit: acfe8c9f1f4145f77653a2bc64a9744b001358dc
---
{{APIRef("DOM")}}
创建新的{{domxref("Text", "文本", "", 1)}}节点。该方法可用于转义 HTML 字符。
## 语法
```js-nolint
createTextNode(data)
```
### 参数
- `data`
- : 包含要放入文本节点的数据的字符串。
### 返回值
{{domxref("Text", "文本", "", 1)}}节点。
## 示例
```html
<!doctype html>
<html lang="zh-CN">
<head>
<title>createTextNode 示例</title>
<script>
function addTextNode(text) {
const newtext = document.createTextNode(text);
const p1 = document.getElementById("p1");
p1.appendChild(newtext);
}
</script>
</head>
<body>
<button onclick="addTextNode('是!');">是!</button>
<button onclick="addTextNode('否!');">否!</button>
<button onclick="addTextNode('我们可以!');">我们可以!</button>
<hr />
<p id="p1">段落第一行。</p>
</body>
</html>
```
{{EmbedLiveSample('示例')}}
## 规范
{{Specifications}}
## 浏览器兼容性
{{Compat}}