我想编写一个脚本,当单击标题时,当它可见时隐藏 div,反之亦然,当隐藏 div 时,它会再次显示。我的建议不起作用,有什么问题?然而,为什么当我将 html 中的 hidden 属性设置为 false 时,我会在显示的页面上得到相反的效果(元素消失,反之亦然,因为这是合乎逻辑的假设)?
function clcHead() {
if (document.getElementById('txt-area').hidden == false) {
document.getElementById('txt-area').hidden = true;
}
if (document.getElementById('txt-area').hidden == true) {
document.getElementById('txt-area').hidden = false;
}
}
<body>
<h1 onclick="clcHead()">Click on header</h1>
<div id="txt-area" hidden="false">
</div>
<script>
</script>
</body>