From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/textencoder/encode/index.html | 74 +++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 files/zh-cn/web/api/textencoder/encode/index.html (limited to 'files/zh-cn/web/api/textencoder/encode') diff --git a/files/zh-cn/web/api/textencoder/encode/index.html b/files/zh-cn/web/api/textencoder/encode/index.html new file mode 100644 index 0000000000..96c91994bd --- /dev/null +++ b/files/zh-cn/web/api/textencoder/encode/index.html @@ -0,0 +1,74 @@ +--- +title: TextEncoder.prototype.encode() +slug: Web/API/TextEncoder/encode +tags: + - 文本编码 + - 编码 +translation_of: Web/API/TextEncoder/encode +--- +
{{APIRef("Encoding API")}}
+ +

TextEncoder.prototype.encode() 方法接受一个 {{domxref("USVString")}} 作为参数,返回一个以给定的文本(字符串)参数,通过 TextEncoder 中指定的方法(默认 UTF-8)编码后的 {{jsxref("Global_Objects/Uint8Array", "Uint8Array")}} 类型的值。

+ +

语法

+ +
b1 = encoder.encode(string);
+
+ +

参数

+ +
+
string
+
一个包含了将要编码的文本的 {{DOMxRef("USVString")}}。
+
+ +

返回值

+ +

一个 {{domxref("Uint8Array")}} 对象。

+ +

示例

+ +
<p class="source">This is a sample paragraph.</p>
+<p class="result">Encoded result: </p>
+ +
const sourcePara = document.querySelector('.source');
+const resultPara = document.querySelector('.result');
+const string = sourcePara.textContent;
+
+const textEncoder = new TextEncoder();
+
+let encoded = textEncoder.encode(string);
+resultPara.textContent += encoded;
+ +

{{EmbedLiveSample('Examples')}}

+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName("Encoding", "#dom-textencoder-encode", "TextEncoder.prototype.encode()")}}{{Spec2("Encoding")}}Initial definition.
+ +

浏览器兼容性

+ +
+ + +

{{Compat("api.TextEncoder.encode")}}

+
+ +

另见

+ + -- cgit v1.2.3-54-g00ecf