繁体   English   中英

javascript遍历div更改背景颜色

[英]javascript iterate through div changing the background colour

我想为每个div自动将背景颜色更改为不同的背景颜色,每个div应该具有不同的颜色。

我不太确定为什么我的html不能正确显示,为什么我会得到[object HTMLCollection]

我的HTML:

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

我使用的功能是:

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解决方案,您无需在函数bgColor()中传递agrumenst;

    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解决方案

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;

为什么这么复杂...

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> 

似乎是一种解决方案https://jsfiddle.net/8rkceLv9/3/您只需选择#box内的所有div并#box设置背景。 我看不到数组中的点。

JavaScript 用于更换<div>按下按钮的颜色</div><div id="text_translate"><p>当我的 html 和 JS 代码在同一个文件中时,我有点不确定为什么我的代码似乎不起作用。 当 html 和 JS 分开时,似乎工作正常。 有人可以指出我的错误....我是新手!</p><p> <strong>HTML:</strong></p><pre> &lt;div class="main"&gt; &lt;div class="light"&gt;&lt;/div&gt; &lt;button onclick="chngCol()" id="burn"&gt;Burn!&lt;/button&gt; &lt;/div&gt;</pre><p> <strong>JavaScript:</strong></p><pre> chngCol() { if(document.getElementByClass('light').style.background == "#00ffff") { document.getElementByClass('light').style.background = "#ffff00"; } else if(document.getElementByClass('light').style.background == "ffff00") { document.getElementByClass('light').style.background = "#ff00ff"; } else if(document.getElementByClass('light').style.background == "#ff00ff") { document.getElementByClass('light').style.background = "#00ffff"; } }</pre><p> <strong>CSS:</strong></p><pre> .light{ width: 50px; height: 50px; background-color:#00ffff; }</pre><p> 所有代码都在同一个文档中,带有适当的标签,但是我在第一个 { 调用 chngCol.</p></div>

[英]JavaScript for changing a <div> colour from a button press

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过Javascript更改图像颜色 使用jQuery更改div的背景色 使用Javascript更改表格行的背景色 使用 css/javascript 更改背景颜色 JavaScript开关,用于使用css()更改背景颜色 JavaScript 用于更换<div>按下按钮的颜色</div><div id="text_translate"><p>当我的 html 和 JS 代码在同一个文件中时,我有点不确定为什么我的代码似乎不起作用。 当 html 和 JS 分开时,似乎工作正常。 有人可以指出我的错误....我是新手!</p><p> <strong>HTML:</strong></p><pre> &lt;div class="main"&gt; &lt;div class="light"&gt;&lt;/div&gt; &lt;button onclick="chngCol()" id="burn"&gt;Burn!&lt;/button&gt; &lt;/div&gt;</pre><p> <strong>JavaScript:</strong></p><pre> chngCol() { if(document.getElementByClass('light').style.background == "#00ffff") { document.getElementByClass('light').style.background = "#ffff00"; } else if(document.getElementByClass('light').style.background == "ffff00") { document.getElementByClass('light').style.background = "#ff00ff"; } else if(document.getElementByClass('light').style.background == "#ff00ff") { document.getElementByClass('light').style.background = "#00ffff"; } }</pre><p> <strong>CSS:</strong></p><pre> .light{ width: 50px; height: 50px; background-color:#00ffff; }</pre><p> 所有代码都在同一个文档中,带有适当的标签,但是我在第一个 { 调用 chngCol.</p></div> 在画布Javascript中更改div的背景颜色 使用JavaScript更改div背景颜色 Javascript:通过动态更改字典进行迭代 如何仅通过第一个div JavaScript进行迭代
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM