【发布时间】:2017-12-20 14:12:37
【问题描述】:
我在一个数组中有 3 个图像,我随机化索引以随机生成它们,但我想生成每个图像 4 次。这样做最有效的方法是什么?
编辑(完整代码):
var image_array = ["image1.png", "image2.png", "image3.png"];
function set_page()
{
var table = document.getElementById("grid");
if(table != null)
{
for(var i = 0; i < table.rows.length; i++)
{
for(var j = 0; j < table.rows[i].cells.length; j++)
{
table.rows[i].cells[j].onclick = function() {
click_cell(this);
}
//table.rows[i].cells[j].style.backgroundImage = "url('../images/back.png')";
add_cell_image(table.rows[i].cells[j]);
}
}
}
}
function click_cell(cell)
{
}
function add_cell_image(cell)
{
for(var i = 0; i <= image_array.length; i++)
{
for(var j = 0; j <= image_array.length; j++)
{
var index = create_values();
cell.innerHTML = "<img class='cell_image' align='middle' width='90' height='90' src ='../images/" + image_array[index] + "'/>";
}
}
}
function create_values()
{
return Math.floor(Math.random() * image_array.length);
}
【问题讨论】:
-
最高效是什么意思?
标签: javascript image random