如何在html / js中逐行折叠/展开(关闭/打开)内容-6`for循环`

时间:2018-07-06 02:13:56

标签: javascript html css for-loop

我想在我的项目中使用以下代码,我要隐藏和显示6个对象列表,是否需要定义6个ID框?(我想用Hide来单独控制这6个ID和Show函数)以及演示“一些文本。一些文本。一些文本”中框的内容。都在for loop中,我可以显示该for loop的完整文本字段吗?因为文本太长,它们会从屏幕上消失。

此外,我可以将Hide设置为默认值,以下代码将显示for loop的内容,无论我进行了什么更改。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .box p {
      width: 360px;
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
    }
    
    .box p:hover {
      overflow: visible;
    }
  </style>
</head>
<body>
  <button id="statusChange">Show</button>
  <div class="box" id="box">
    <p>1、Some texts. Some texts. Some texts. Some texts. Some texts. Some texts. Some texts. Some texts. </p>
    <p>2、Some texts. Some texts. Some texts. Some texts. Some texts. Some texts. Some texts. Some texts.</p>
    <p>3、Some texts. Some texts. Some texts. Some texts. Some texts. Some texts. Some texts. Some texts.</p>
    <p>4、Some texts. Some texts. Some texts. Some texts. Some texts. Some texts. Some texts. Some texts.</p>
    <p>5、......</p>
  </div>
  <script>
    document.getElementById('statusChange').onclick = function() {
      var text = this.innerText
      if (text == 'Hide') {
        document.getElementById('box').style.cssText = 'display:none'
        this.innerText = 'Show'
      } else if (text == 'Show') {
        document.getElementById('box').style.cssText = 'display:block'
        this.innerText = 'Hide'
      }
    }
  </script>
</body>
</html>

0 个答案:

没有答案