由于某些原因,以下javascript代码无法正确更改所选列表的图像,但是当您检查元素时,请关闭检查元素窗口,关闭浏览器底部的下载窗口,或者选择另一个元素,然后返回列表中最初选择的元素,将显示图像。另外,在Firefox上,我没有遇到这种情况(仅在Chrome上发生);从下拉菜单中选择一个元素,即可加载所需的图像。
在Chrome上:
在Firefox上:
Javascript:
villainChoice = document.getElementById("villainChoice");
villainChoice.onchange = function () {
if (villainChoice.value == "") {
document.getElementById("villainWaitingHand").innerHTML = "";
document.getElementById("villainWaitingHand").classList.remove("bodydiv");
document.getElementById("villainWaitingHand").classList.add("hidden");
} else {
document.getElementById("villainWaitingHand").classList.remove("hidden");
document.getElementById("villainWaitingHand").classList.add("bodydiv");
var vilName = villainChoice.value.toLowerCase().split(' ').join('_').split('.').join('');
document.getElementById("villainWaitingHand").innerHTML = "<p>" + villainChoice.value + " is ready.</p><br><img id='" + vilName + "' class='villainWaitingImg' src='/images/" + vilName + "_waiting.svg'>";
}
}
HTML:
<div id="throw_choice" class="bodydiv">
<form id="gameForm" action="/" method="get">
Enter your choice of villain:<br>
<select id="villainChoice" name="villain" required>
<option value=""> </option>
<option value="Bones">Bones</option>
<option value="Gato">Gato</option>
<option value="Manny">Manny</option>
<option value="Mr. Modern">Mr. Modern</option>
<option value="Regal">Regal</option>
<option value="The Boss">The Boss</option>
<option value="Comic Hans">Comic Hans</option>
<option value="Harry">Harry</option>
<option value="Mickey">Mickey</option>
<option value="Pixie">Pixie</option>
<option value="Spock">Spock</option>
<option value="The Magician">The Magician</option>
</select>
<br><br>
<button type = "submit" id="throw_choice_button">Throw Choice</button>
</form>
</div>
<div id="villainWaitingHand" class="hidden"></div>