开发者

javascript array into a div

开发者 https://www.devze.com 2023-02-22 12:35 出处:网络
I just need a little tweak, I know all this code isn\'t perfect (far from it). Instead of writing the contents of the array into a div, I ideally want to create a new div for each number in the array

I just need a little tweak, I know all this code isn't perfect (far from it). Instead of writing the contents of the array into a div, I ideally want to create a new div for each number in the array and then add it to the card container?

balls90 = [ '1', '2', '3', '4', '5', '6','7','8','9','10','11', '12', '13', '14', '15', '16','17','18','19','20','21', '22', '23', '24', '25', '26','27','28','29','30','31', '32', '33', '34', '35', '36','37','38','39','40','41', '42', '43', '44', '45', '46','47','48','49','50','51', '52', '53', '54开发者_高级运维', '55', '56','57','58','59','60','61', '62', '63', '64', '65', '66','67','68','69','70','71', '72', '73', '74', '75', '76','77','78','79','80','81', '82', '83', '84', '85', '86','87','88','89','90' ];

function getNumbers(){

    var player1 = new Array();

    balls90.sort( function() { return Math.random() - .25 } );

    for ( var i=1; i<=12; i++ ) {   
        player1.push(balls90[i]);
        document.getElementById("cardContainer").innerHTML+=(balls90[i]);
    }

}


Simple method, instead of appending to the div try appending to a variable, something like

var html='';
for (var i=1; i<=12; i++) {
    html+='<div>'+balls90[i]+'</div>';
}
document.getElementById('cardContainer').innerHTML+= html;


You can easily use document.createElement("div") and appendChild()

var div = document.createElement("div");
div.innerHTML = (balls90[i]);
cardContainer.appendChild(div);

Example on jsfiddle.

Using this method will get you away from having to worry about yucky string concatenation.


You can accomplish this with element.createChild() and element.appendChild().


Try this

                var full_list = "";
                for(var i=0; i<balls90.length; ++i){
                    full_list = full_list + balls90[i]+ "<br>";
                } 

                $("#cardContainer").html(full_list);


Dynamically create a div with var div = document.createElement('div') and append the array iteration (values) to a dynamically created node var node = document.createTextNode(value[i]) . Just store the dynamically created DOM-Element into a variable like var div. You can do the same to the dynamically created node. Then you can append the node to the DOM-Element with div.appendChild(node) and append the whole to the Parent Element.


document.getElementById("cardContainer").innerHTML += ('<div>'+balls90[i]+'</div>');

If you want each number in its own div, just wrap each number in <div></div>

jsFiddle: http://jsfiddle.net/UjsgY/


Like this?

balls90 = [];
for (var i=0;i<90;i++)balls90[i]=(i+1);

function getNumbers(){
  var player1 = new Array();
  balls90.sort( function() { return Math.random() - .25 } );
  var container = document.getElementById("cardContainer")
  var div;
  for ( var i=1; i<=12; i++ ) { 
    player1.push(balls90[i]);
    div = document.createElement('div');
    div.innerHTML=balls90[i];
    container.appendChild(div)
  }
}


If I understand correctly, you want to create a div for each element in the array. You may (not) be familiar with array.join(). You will need a container to put the contents in, I will create one then create the individual divs with array.join().

var container = document.createElement('div');
//A string containing a series of divs and the contents of the Array.
container.innerHTML = '<div>' + balls90.join('</div><div>') + '</div>';  
//append div to body last, to create less DOM refreshes
document.body.appendChild( container ); 


// Lots of ways to do this, here's another

var div= document.createElement('div'),
txt= document.createTextNode('1'), i= 1, tem,
pa= document.createDocumentFragment();
div.appendChild(txt);
pa.appendChild(div);
while(i<90){
    tem= div.cloneNode(true);
    tem.firstChild.data=++i;
    pa.appendChild(tem);
}
document.getElementById("cardContainer") .appendChild(pa);
0

精彩评论

暂无评论...
验证码 换一张
取 消