HTML DOM Body 属性
HTML DOM body 属性与 HTML <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>输出
它将生成以下输出 −

单击“Overwrite Content” −

在上面的示例中 −
我们在 <body> 元素中有许多元素,如 <h2> 元素、两个 <p> 元素和一个按钮。“Overwrite Content” 按钮执行 OverWrite() 函数。
<button>Overwrite Content</button>
OverWrite() 函数获取文档正文的 innerHtml 并将文本更改为“All the content inside the body tag is overwritten”。这样就可以覆盖 body 内部所有内容,基本上就是消除所有元素,仅显示文本。
function OverWrite() {
document.body.innerHTML = "All the content inside the body tag is overwritten";
}
广告
数据结构
网络技术
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP