html dom characterset属性表示与元素的charset属性相关联的字符集。默认情况下,html文档的字符集为utf-8。
characterSet属性以字符串格式返回HTML文档的字符编码。用户可以使用HTML中的charset属性或DOM 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><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/cb6835dc7db1" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">前端免费学习笔记(深入)</a>”;</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。然后,使用paragraph元素的innerHTML()方法将编码显示在id为“Sample”的段落元素中,并将一些文本和附加的变量x分配给它 −
function encode() {
var x = document.characterSet;
document.getElementById("Sample").innerHTML = "The character encoding used is "+ x;
}以上就是HTML DOM characterSet 属性 HTML DOM characterSet 属性返回当前文档的字符编码集的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号