HTML DOM Body 属性
htmlweb developmentfront end technology更新于 2024/6/11 10:20:17
与 HTML <body> 元素关联的 HTML DOM body 属性用于设置或返回 <body> 元素的属性值。它返回 <body> 元素。它可用于更改 <body> 元素内的内容。此属性可以覆盖 <body> 元素内现有的子元素内容。
语法
以下是语法 −
设置 body 属性 −
document.body = New_Content
此处,New_Content 是元素的新内容。
示例
让我们看一个 HTML DOM Body 属性的示例 −
<!DOCTYPE html>
<html>
<body>
<h2>Sample HEADING</h2>
<p>Click the below button to overwrite child content</p>
<button onclick="OverWrite()">Overwrite Content</button>
<p>A sample paragraph</p>
<script>
function OverWrite() {
document.body.innerHTML = "All the content inside the body tag is overwritten";
}
</script>
</body>
</html>
输出
这将产生以下输出 −

点击"覆盖内容"时 −

在上面的例子中 −
<body> 元素内包含各种元素,例如一个 <h2> 元素、两个 <p> 元素和一个按钮。"覆盖内容"按钮会执行 OverWrite() 函数。
<button>覆盖内容</button>
OverWrite() 函数获取文档正文的 innerHtml 内容,并将文本更改为"body 标签内的所有内容均被覆盖"。这将覆盖正文内的所有内容,基本上会删除所有元素,只显示文本。
function OverWrite() {
document.body.innerHTML = "All the content inside the body tag is overwritten";
}
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

