<html>
<body>
<button onclick="document.getElementById('myImage').src='hagthrowingball.gif'">Throw Fire ball</button>
<img id="myImage" src="foresthag.gif" style="width:100px">
</body>
</html>
我需要图像src onclick来显示图像hagthrowingball.gif,但是然后返回到foresthag.gif而无需再次按下按钮。
我认为这应该通过一段时间后自动将旧的src设置为原值来解决您的问题;
<button onclick="replaceFunc();">Throw Fire ball</button>
<img id="myImage" src="foresthag.gif" style="width:100px">
<script>
function replaceFunc (){
const el = document.getElementById("myImage");
const oldSrc = el.src;
el.src = 'hagthrowingball.gif';
setTimeout(()=>el.src=oldSrc, 1000);
}
</script>
</body>
</html>