所以在我的HTML中,我有一个包含64个标签的表,我想做的是让10个随机显示某个图片。这就是我到目前为止所拥有的。我如何在里面显示图像? (仅限Javascript)。提前谢谢。
<script type = "text/javascript">
var img = document.createElement("img");
img.src = "sweeperbomb.png";
function generateBombs()
{
constants = [1,2,3,4,5,6,7,8,9,]
numOfBombs = 0;
BOMB = 9;
while (numOfBombs != 10)
{
i = Math.floor(Math.random()*constants.length);
if (constants[i] != BOMB)
{
constants[i] = BOMB;
numOfBombs++;
}
}
}
</script>
答案 0 :(得分:1)
这是完全未经测试但应该做好工作,祝你好运
function generateBombs()
{
var targetTable = document.getElementsByTagName('table')[0],
used = [],
bomb = 0,
x,
y;
while (bomb <= 9) {
y = Math.round(Math.random() * 7);
x = Math.round(Math.random() * 7);
if (used.indexOf(y + ':' + x) === -1) {
targetTable.getElementsByTagName('tr')[y]
.getElementsByTagName('td')[x]
.innerHTML = '<img src="bomb.png" />';
used.push(y + ':' + x);
bomb++;
}
}
}
generateBombs();
<强>的jsfiddle 强>
http://jsfiddle.net/pfxpcoe0/