【发布时间】:2016-04-05 09:33:12
【问题描述】:
这是我的小提琴https://jsfiddle.net/tuc1faug/1/ 在这里,我使用 jquery 为颜色分配了特定的值。 每次颜色都会被打乱。现在我希望将这些值隐藏在单元格中,并且我希望所有这些值都以打乱的顺序存储到数组中 html:
<table border="5px" width="500px" height="50px" align="center">
<tr id="colors">
<td height="50px" orderId="1" bgcolor="red"></td>
<td height="50px" orderId="6" bgcolor="brown"></td>
<td height="50px" orderId="5" bgcolor="pink" ></td>
<td height="50px" orderId="0" bgcolor="blue" ></td>
<td height="50px" orderId="7" bgcolor="black"></td>
<td height="50px" orderId="2" bgcolor="green"></td>
<td height="50px" orderId="4" bgcolor="orange" ></td>
<td height="50px" orderId="3" bgcolor="yellow"></td>
</tr>
</table>
jQuery:
var arr=[];
var colorCells =document.getElementById('colors').getElementsByTagName('td');
var colors = ["blue","red","green","yellow","orange","pink","brown","black"];
for(var i = 0; i < colorCells.length; i++) {
$(colorCells[i]).attr("bgColor", colors.splice(Math.random() * (colors.length),1)) ;
arr.push(colorCells[i].style.backgroundColor);
}
var colorValues = {"red": 2, "blue":3, "green": 4, "yellow":"1", "orange":5, "black":1, "brown":6, "pink":5};
$("table td").each(function() {
$(this).html(colorValues[$(this).attr("bgColor")]);
});
【问题讨论】:
-
现在颜色值是硬编码的吗?说
red是2? -
每当我改变它时它可能会改变
-
你能解释一下你在做什么。我不确定我是否跟随。
-
@Preethi 所以你正在从
java variable填充这个数组,正如我们看到的here -
`我希望所有这些值都以打乱顺序存储到一个数组中 Html:`这是什么意思?
标签: jquery html shuffle assign