javascript - 如何设置初始值为 'Hidden'

标签 javascript html css

我是 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>

Fiddle Link

最佳答案

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/

相关文章:

javascript - 有什么方法可以让 div 在 ipad 中滚动吗?

javascript - __proto__ VS。 JavaScript 中的原型(prototype)

javascript - 如何将 "Different"多个 <Button> 函数转换为带有 <Select> 的通用函数?

css - 如何在 Qt 样式中平滑缩小背景图像?

css - 部分图像被隐藏,没有溢出

javascript - 如何计算接近无穷大或 0 的极限?

javascript - 使用node.js将url转发到游戏服务器

python - 读取 html 文件并将其显示在 tkinter 窗口中

Javascript等待函数完成调用另一个内部函数

javascript - 脚本和 CSS 是否也支持 HTML <base> 标签?