简体   繁体   English

重复div次

[英]Repeat a div number of times

how to repeat a div X times over html page, lets say I want to set variance to declare times of repeat. 如何在html页面上重复div X次,假设我想设置方差来声明重复次数。 repeat this section 5 times, I assume it's with JS. 重复本节5次,我认为是JS。

<div class="blackstrip">black</div>
<div class="bluestrip">
    BLUE
    <div class="whitestrip">WHITE strip</div>
</div>

I'm adding the css as it comes out strange look - 我正在添加css,因为它出来奇怪的样子 -

.blackstrip{
opacity: 0.8;
width: 600px;
height: 100px;
background-color: black;
background-position: center;
padding-top: 10px;
padding-bottom: 10px;
text-align: center;
margin: 0 auto;

} }

.bluestrip{
opacity: 0.5;
width: 600px;
height: 300px;
background-color: blue;
background-repeat: none;
padding-bottom: 10px;
position: relative;
margin-top:50px;
margin: 20px auto;

} }

.whitestrip{
opacity: 0.8;
width: 100px;
height:300px;
background-color: gray;
position: relative;
float: right;
padding-top: 10px;
padding-bottom: 10px;
}

Just use a for loop eg: 只需使用for循环,例如:

var amount = 5;
for (var i = 0; i < amount; i++){
    var new_div = document.createElement("div");
    new_div.className = "bluestrip";
    document.body.appendChild(new_div);
    console.log("This is repeat " + i);
}

Simple technic, huh? 简单的技术,对吧?

https://jsfiddle.net/d15e9r6z/ https://jsfiddle.net/d15e9r6z/

Like Andreas said, it is more effective if you use DocumentFragment to change multiple DOM's. 就像Andreas所说的那样,如果你使用 DocumentFragment 来改变多个DOM ,它会更有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM