简体   繁体   中英

javascript iterate through div changing the background colour

I would like to automatically change the background-color to a different background-color for each div, each div should have a different colour.

I'm not entirely sure why my html is not correctly displayed and why I get [object HTMLCollection]

my HTML:

<div id="box">
    <h3>box</h3>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</div>

The function I use is:

bgColor(document.getElementById('box').getElementsByTagName('div'));

    function bgColor() {

      var tableCntr= document.getElementById('box');
      var tblHTML = document.getElementsByTagName('div');

      var colors = [['red','blue','green'],['orange', 'black', 'purple']]  ;

        for(var i=0;i<colors.length;i++) {

          for(var j=0;j<colors[0][0].length;j++) {
            tblHTML += "<div style='background:" + colors[i][j] + "'> </div>" ;
          }
       }
      tableCntr.innerHTML = tblHTML ;  
    }

    window.onload = bgColor;

jsfiddle

javascript solution, you dont need to pass agrumenst in the function bgColor();

    bgColor();
    function bgColor() {

    var tableCntr= document.getElementById('box');
    var tblHTML = '';//document.getElementsByTagName('div');

    var colors = [['red','blue','green'],['orange', 'black', 'purple']]  ;

     for(var i=0;i<colors.length;i++) {
         for(var j=0;j<colors[0][0].length;j++) {
            tblHTML += "<div style='background:" + colors[i][j] + "'> </div>" ;
         }
     }
     tableCntr.innerHTML = '<h3>box</h3>'+tblHTML ;  
    }

   window.onload = bgColor;

jQuery Solution

bgColor();

 function bgColor() {

  var tableCntr = jQuery('#box');
  var tblHTML = '';
  var colors = [['red','blue','green'],['orange', 'black', 'purple']]  ;

    for(var i=0;i<colors.length;i++) {
      for(var j=0;j<colors[0][0].length;j++) {
        tblHTML += "<div style='background:" + colors[i][j] + "'> </div>" ;
      }
   }
  tableCntr.html( tblHTML );  
}

window.onload = bgColor;

Why so complicated....

function bgColor(ID) {

  var tableCntr= document.getElementById(ID);
  var DIVs = tableCntr.getElementsByTagName('div');

  var colors = ['red','blue','green','orange', 'black', 'purple']  ;

  var colourIndex = 0;
  for(var i=0;i<DIVs.length;i++) {

     DIVs[i].setAttribute('style','background-color: '+colors[colourIndex]);
     colourIndex = (colourIndex + 1) % colors.length;
  }
}

bgColor('box');

 function bgColor(id) { var divs = document.getElementById(id).getElementsByTagName('div'), colors = ['red','blue','green','orange', 'black', 'purple']; [].forEach.call(divs, function(d, index) { d.setAttribute('style', 'background-color: ' + colors[index % colors.length]); }); } bgColor('box'); 
 #box div { width: 30px; height: 30px; margin-right: 5px; margin-bottom: 1px; background-color: black; } 
 <div id="box"> <h3>box</h3> <div>&nbsp;</div> <div>&nbsp;</div> <div>&nbsp;</div> <div>&nbsp;</div> <div>&nbsp;</div> <div>&nbsp;</div> <div>&nbsp;</div> <div>&nbsp;</div> <div>&nbsp;</div> <div>&nbsp;</div> <div>&nbsp;</div> <div>&nbsp;</div> <div>&nbsp;</div> <div>&nbsp;</div> <div>&nbsp;</div> <div>&nbsp;</div> <div>&nbsp;</div> </div> 

Seems like a solution https://jsfiddle.net/8rkceLv9/3/ You just select all divs inside #box and set them a background. I don't see the point in array in array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM