study reflow onceagain
Today I met with a problem about dynamic adding pictures with js.1
2
3
4
5
6
7<div id="main">
<div class="box">
<div class="pic">
<img src="img/{{index}}.jpg">
</div>
</div>
</div>
I want to add my pictures in “{index}”,so many that I can’t make many “div”s.I think of JS.1
2
3
4
5
6
7
8
9
10
11
12
13
14//1get template
var _img=$("#main").innerHTML
.replace(/^\s*/,"")
.replace(/\s*$/,"");
//2 var
var out_img=[];
//3add data
for(var i in data){
var html_img=_img
.replace(/{{index}}/g,data[i].img);
out_img.push(html_img);
}
//4reflow
$("#main").innerHTML+=out_img.join("");