最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - creating multiple vertical lines in HTML using loops - Stack Overflow

programmeradmin0浏览0评论

I'm new to JS and would like to create 10 vertical lines in my webpage. I have written my HTML code as

  <div id="verticle-line"></div>

and in my CSS I have

#verticle-line {
width: 1px;
min-height: 400px;
background: red;  
margin:15px;  
float:left
}

How Can I create 10 such lines in my webpage using JavaScript?

I'm new to JS and would like to create 10 vertical lines in my webpage. I have written my HTML code as

  <div id="verticle-line"></div>

and in my CSS I have

#verticle-line {
width: 1px;
min-height: 400px;
background: red;  
margin:15px;  
float:left
}

How Can I create 10 such lines in my webpage using JavaScript?

Share Improve this question edited May 27, 2016 at 5:50 Augustus asked May 5, 2016 at 11:27 AugustusAugustus 1,5092 gold badges17 silver badges31 bronze badges 3
  • check this link : stackoverflow./questions/6304453/… – SamGhatak Commented May 5, 2016 at 11:30
  • stackoverflow./questions/37049408/… – Jinu Kurian Commented May 5, 2016 at 11:50
  • Possible duplicate of Create a div using loop – Mohammedshafeek C S Commented May 5, 2016 at 11:50
Add a ment  | 

2 Answers 2

Reset to default 4

There are many ways to do this but the easiest would probably be this:

for(var i=0; i<10; i++) {
  document.write('<div class="verticle-line"></div>');
}

Use a for loop to write 10 divs on your page. I also changed id to class, because you should not have more than one element with the same id on your page. Make sure you change your CSS to match a class.

See this -

for(x=0; x<9;x++) {
    var vertical = document.createElement('div');
    vertical.className = "verticle-line";
    document.getElementById('wrapper').appendChild(vertical);
}
.verticle-line {
  width: 1px;
  min-height: 400px;
  background: red;
  margin: 15px;
  float: left
}
<div id="wrapper">
  <div class="verticle-line"></div>
</div>

发布评论

评论列表(0)

  1. 暂无评论