HTML DOM characterSet 属性
htmlweb developmentfront end technology更新于 2025/9/23 3:24:17
HTML DOM characterSet 属性表示与 <meta> 元素的 charset 属性关联的字符集。默认情况下,HTML 文档的字符集为 UTF-8。
characterSet 属性以字符串格式返回 HTML 文档的字符编码。用户可以使用 HTML 中的 charset 属性或 DOM characterSet 属性覆盖网页的默认 characterSet。
语法
以下是 characterSet 属性的语法 −
document.characterSet
示例
让我们看一个 HTML DOM characterSet 属性的示例 −
<!DOCTYPE html>
<html>
<body>
<p>Click the below button to know the encoding of this HTML document</p>
<button onclick="encode()">CHECK ENCODE</button>
<p id="Sample"></p>
<script>
function encode() {
var x = document.characterSet;
document.getElementById("Sample").innerHTML = "The character encoding used is "+ x;
}
</script>
</body>
</html>
输出
这将产生以下输出 −

点击"CHECK ENCODE"按钮 −

在上面的例子中 −
我们首先创建了一个按钮"CHECK ENCODE",当用户点击该按钮时,它将执行 encode() 函数 −
<button onclick="encode()>CHECK ENCODE</button>
encode() 方法将使用文档的 characterSet 属性获取文档的字符编码,并将其赋值给变量 x。然后使用段落上的 innerHTML() 方法将编码显示在 id 为"Sample"的段落元素中,并为其分配一些文本和附加变量 x −
function encode() {
var x = document.characterSet;
document.getElementById("Sample").innerHTML = "The character encoding used is "+ x;
}
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

