簡體   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