我是 following this example创建一段文本,单击该文本会展开/折叠文本。
如何设置初始值隐藏,让段落一开始是折叠的,点击时会展开?
下面是代码。
div#expand {
width: 500px
display:none;
}
<html lang="en">
<head>
<meta charset="utf-8">
<script>
function show() {
if (document.getElementById('expand').style.display == 'none')
document.getElementById('expand').style.display = 'block';
else
document.getElementById('expand').style.display = 'none';
}
</script>
</head>
<body>
<p>
<a href="javascript:;" onclick=show()>About Us</a>
<div id="expand">
<p>this is all about us. <br></p>
</div>
</p>
</body>
</html>
最佳答案
document.getElementById('expand').style
将检查不是来自您的 css 文件的内联书写样式...因此请尝试编写内联 css display:none;
到那个元素。
你也不能编码 <p>
在另一个标签内 <p>
标签...无效。
你还错过了;
在 width:500px
之后在你的 CSS 中
function show() {
if (document.getElementById('expand').style.display == 'none')
document.getElementById('expand').style.display = 'block';
else
document.getElementById('expand').style.display = 'none';
}
div#expand {
width: 500px;
}
<a href="javascript:void(0)" onclick=show()>About Us</a>
<div id="expand" style="display:none;">
<p>this is all about us.<br></p>
</div>
关于javascript - 如何设置初始值为 'Hidden',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49605437/