<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#div1 {
width: 100px;
height: 100px;
background: pink;
display: block;
}
</style>
</head>
<body>
<!-- 點擊按鈕顯示隱藏 -->
<input type="button" value="顯示隱藏" id="btn" onclick="showHide()">
<div id="div1"></div>
<script>
function showHide() {
var odiv = document.getElementById('div1');
if (odiv.style.display == 'none') {
odiv.style.display = 'block'
} else {
odiv.style.display = 'none'
}
}
</script>
</body>
</html>
運行結果