字符串拼接的两种方法

1.普通的拼接:

1         var htmlSpan = "";
2           for (var j = 0; j < len - 1; j++) {
3               htmlSpan += '<span>我是拼接生成的字符串</span>';
4           }
5           htmlSpan += '<span class="on">·</span>';
6           $(".dots").html(htmlSpan);

2.使用数组的方法:

 1   var data = [
 2         {id:'1'},
 3         {id:'2'},
 4         {id:'3'},
 5         {id:'4'},
 6     ]
 7     test(data)
 8     function test(msgArr) {
 9         var len = msgArr.length;
10         var n = 0 ;
11         var imgs = [];
12         var img = [];
13         img[0] = '<a href="' + 222 + '/webservice/dish/detail?id=';
14         img[1] = '#';
15         img[2] = '"><img src="';
16         img[3] = '#';
17         img[4] = '"></a>';
18         for (var i = 0; i < len; i++) {
19             img[1] = msgArr[i].id;
20             img[3] = msgArr[i].pic;
21             imgs.push(img.join(""));
22         }
23         $("#banner_list").html(imgs.join(""));
24     }

 

posted @ 2018-10-30 21:27  前端极客  阅读(9413)  评论(0编辑  收藏  举报