Create divs from Array elements

前端 未结 4 763
孤独总比滥情好
孤独总比滥情好 2020-12-05 15:43

I have different strings in my array. Is it possible to create a divs like

for each array element in my HTML page?

4条回答
  •  自闭症患者
    2020-12-05 16:05

    You can use

    var results = ['result 1', 'result 2', 'result 3'];
    
    $.each(results, function(idx, value){
      var newdiv = $('
    ',{class: 'results', text: value}); $('body').append( newdiv ); });

    used $.each() docs, and also changed the id to class since id needs to be unique.

    demo at http://jsfiddle.net/gaby/FuPF7/

提交回复
热议问题