我认为我的 if/else 语句有问题,导致此代码不起作用。我曾尝试更改 id 并使用 document.canvas.src 而不是 getElementById 但它对我来说无论哪种方式都不起作用。
更新:下面是我用来让它正常运行的代码:
<HTML>
<HEAD>
<script type = "text/javascript">
function displayImage(whichImage)
{
if(whichImage != "noImage")
{
document.canvas.src= whichImage;
}
else{
document.canvas.src= "blank.jpg";
}
}
</script>
</HEAD>
<BODY>
<form name="imageForm">
<table border=3>
<tr>
<td>
<select name="imageSelector" onChange="displayImage(this.value)">
<option value="noImage">Select an Animal
<option value="dog.jpg">Dog
<option value="cat.jpg">Cat
<option value="parrot.jpg">Parrot
<option value="fish.jpg">Fish
<option value="alligator.jpg">Alligator
<option value="mouse.jpg"> Mouse
<option value="fox.jpg">Fox
</select>
</td>
</tr>
<tr>
<td>
<img src="blank.jpg" name="canvas" id="animal">
</td>
</tr>
</table>
</form>
</BODY>
</HTML>