HTML DOM documentElement 属性
htmlweb developmentfront end technology更新于 2024/6/1 14:04:17
HTML DOM documentElement 属性用于返回文档元素。返回类型为 Element 对象。文档元素是文档的根元素,在 HTML 文档中为 <HTML> 元素。它是一个只读属性。
语法
以下是 documentElement 属性的语法 −
document.documentElement
示例
让我们来看一个 documentElement 属性的示例 −
<!DOCTYPE html>
<html>
<body>
<h1>documentElement property example</h1>
<p>Get the document element name by clicking the below button</p>
<button onclick="getDocument()">GET NAME</button>
<p id="Sample"></p>
<script>
function getDocument() {
var dName = document.documentElement.nodeName;
document.getElementById("Sample").innerHTML = "The document element for this document is: "+dName;
}
</script>
</body>
</html>
输出
这将产生以下输出 −

点击"获取名称"按钮 −

在上面的例子中 −
我们创建了一个按钮 GET NAME,当用户点击该按钮时,它将执行 getDocument() 函数。
<button onclick="getDocument()">GET NAME</button>
getDocument() 函数获取文档的 documentElement nodeName 属性并将其赋值给变量 dName。然后,变量 dName 显示在与其关联的 id 为"Sample"的段落中,并使用 innerHTML 属性显示预期文本 −
function getDocument() {
var dName = document.documentElement.nodeName;
document.getElementById("Sample").innerHTML = "The document element for this document is: "+dName;
}
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

