【问题标题】:Creating dynamic divs in a for loop in JavaScript在 JavaScript 的 for 循环中创建动态 div
【发布时间】:2020-05-24 05:02:38
【问题描述】:

尝试循环遍历 DOM 中的 div 并动态渲染 300 次。在样式化元素中访问 HTML 时遇到问题。 代码如下:

Current Render

JavaScript 文件:

var amount = 5;
for(var i = 0; i < amount; i++){
    var new_div = document.createElement("div");
    new_div.className = "hello";
    document.body.appendChild(new_div).innerHTML;

    console.log("This is repeat " + i)
}

HTML 文件:

<!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>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="hello">
      <p>
        Hello World!
      </p>
    </div>
    <script src="app.js"></script>
  </body>
</html>

CSS 样式表:

.hello {
  width: 80%;
  margin: 0 auto;
  padding: 20px;
  text-align: center;
  border-style: outset;
  border-width: 5px;
  border-color: #d36135;
}

我希望能够多次渲染第一个 div,因为它的样式完全一致。

【问题讨论】:

    标签: javascript html loops for-loop dom


    【解决方案1】:

    为什么要在 append 方法的末尾访问 innerHTML?没有必要这样做,这会导致问题。

    工作示例:

    var amount = 5;
    for(var i = 0; i < amount; i++){
        var new_div = document.createElement("div");
        new_div.className = "hello";
        document.body.appendChild(new_div);
    
        console.log("This is repeat " + i)
    }
    .hello {
      width: 80%;
      margin: 0 auto;
      padding: 20px;
      text-align: center;
      border-style: outset;
      border-width: 5px;
      border-color: #d36135;
    }
    <!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>
        <link rel="stylesheet" href="style.css" />
      </head>
      <body>
        <div class="hello">
          <p>
            Hello World!
          </p>
        </div>
        <script src="app.js"></script>
      </body>
    </html>

    【讨论】:

      【解决方案2】:

      您的代码运行良好,但您似乎想克隆第一个元素

      var amount = 5;
      
      for (var i = 0; i < amount; i++) {
        let elem = document.getElementById("hello").cloneNode(true);
        elem.id = 'hello_' + i;
        document.body.appendChild(elem);
      }
      .hello {
        width: 80%;
        margin: 0 auto;
        padding: 20px;
        text-align: center;
        border-style: outset;
        border-width: 5px;
        border-color: #d36135;
      }
      <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>
        <link rel="stylesheet" href="style.css" />
      </head>
      
      <body>
        <div class="hello" id='hello'>
          <p>
            Hello World!
          </p>
        </div>
        <script src="app.js"></script>
      </body>
      
      </html>

      【讨论】:

        猜你喜欢
        • 2020-09-28
        • 1970-01-01
        • 1970-01-01
        • 2021-05-06
        • 2019-06-14
        • 2012-07-23
        • 1970-01-01
        • 2016-03-09
        • 1970-01-01
        相关资源
        最近更新 更多