var ar = ["blue", "green", "red"],
x = document.getElementsByTagName('DIV'),
i,
colors = {};
colors[ar[0]] = 'blue';
colors[ar[1]] = 'green';
colors[ar[2]] = 'red';
for (i = 0 ; i < x.length ; i++) {
x[i].style.backgroundColor = colors[x[i].className];
}
.one_in,.two_in ,three_in{ width:100px; height:100px; border:1px solid #000; }
<div class="blue one_in">
</div><div class="green two_in">
</div><div class="one_in">
</div><div class="red "></div>
爲什麼不工作,當我把多個類和空間,如果我把空白的陣列["blue" + " ", "green" + " ", "red"+ " "]
,does not工作如何實現這一目標?
當我從div
刪除班,這會再次工作?
第一件事,缺少*' 。*在'.one_in,.two_in,three_in'中的'three_in'前面 –