HTML DOM Style visibility 属性
htmlweb developmentfront end technology更新于 2025/6/11 8:12:17
HTML DOM Style visibility 属性用于返回并修改元素在 HTML 文档中是否可见。
语法
以下是语法 −
返回可见性
object.style.visibility
修改可见性
object.style.visibility = “value”
值
此处,值可以是负号。
| 值 | 说明 |
|---|---|
| initial | 将此属性值设置为默认值。 |
| inherit | 从其父元素继承此属性值。 |
| hidden | 在这种情况下,元素不可见,但其存在仍然会影响布局。 |
| visible | 在这种情况下,元素可见。 |
| collapse | 在这种情况下,表格行或列中的元素不可见,但其存在会影响布局。 |
示例
让我们看一个 HTML DOM Style visibility 属性的示例 −
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: #000;
background: lightblue;
height: 100vh;
}
.btn {
background: #db133a;
border: none;
height: 2rem;
border-radius: 2px;
width: 40%;
display: block;
color: #fff;
outline: none;
cursor: pointer;
margin: 1rem 0;
}
</style>
</head>
<body>
<h1>DOM Style visibility Property Example</h1>
<p>I'm paragraph element with some dummy text</p>
<button onclick="add()" class="btn">Set visibility</button>
<script>
function add() {
document.querySelector('p').style.visibility = "hidden";
}
</script>
</body>
</html>
输出
这将产生以下输出 −

点击“设置可见性”按钮,将段落元素的可见性设置为隐藏值 −

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

