我正在编写一个基本上翻转硬币的javascript函数,我试图让它返回图像,但到目前为止还没有运气。我使用 img src 标签为变量赋值,但不知何故我怀疑这不起作用,我想和你们核实一下这是如何完成的?
这是我的代码:
<script type = text/javascript>
var headsCount = 0;
var tailsCount = 0;
function start()
{
var buttonTwo = document.getElementById("coinButton");
buttonTwo.addEventListener("click", coinResult, false);
}
function flip()
{
var faceValue = Math.floor((Math.random()*2)+1);
if (faceValue == 1)
{
headsCount++;
return true;
}
else
{
tailsCount++;
return false;
}
}
function coinResult()
{
var result = document.getElementById("coinToss");
var coinCount = document.getElementById("countNumber");
<img name = "Heads" src = "images/Heads.png" />
if (flip() == true)
{
result.innerHTML = <img src = "images/Heads.png" alt = "Heads" />
}
else
{
result.innerHTML = <img src = "images/Tails.png" alt = "Tails" />
}
coinCount.innerHTML = "Heads Count: " + headsCount + " Tails Count: " + tailsCount;
}
window.addEventListener("load", start, false);
</script>