如何用 JavaScript 设置元素的 display 类型?
要设置元素的显示类型,请使用 display 属性。如果你想隐藏元素,请使用 none。
示例
你可以尝试运行以下代码,以用 JavaScript 设置元素的显示类型 −
<!DOCTYPE html>
<html>
<head>
<style>
#myID {
width: 250px;
height: 200px;
background-color: green;
}
</style>
</head>
<body>
<button onclick="display()">Set display as none</button>
<div id="myID">
<h1>Heading Level 1 for Demo</h1>
This is Demo Text. This is Demo Text. This is Demo Text. This is Demo Text.
</div>
<script>
function display() {
document.getElementById("myID").style.display = "none";
}
</script>
</body>
</html>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP