我按下按钮一次后如何使图像切换回原始图像 src

<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 来显示图像 hagthroingball.gif,但然后返回 forforethag.gif,而无需再次按按钮。


汪汪一只猫
浏览 74回答 1
1回答

元芳怎么了

我认为这应该可以通过在一段时间后自动设置旧的 src 来解决您的问题;<button onclick="replaceFunc();">Throw Fire ball</button><img id="myImage" src="foresthag.gif" style="width:100px"><script>&nbsp; function replaceFunc (){&nbsp; &nbsp; &nbsp; const el = document.getElementById("myImage");&nbsp; &nbsp; &nbsp; const oldSrc = el.src;&nbsp; &nbsp; &nbsp; el.src = 'hagthrowingball.gif';&nbsp; &nbsp; &nbsp; setTimeout(()=>el.src=oldSrc, 1000);&nbsp; }</script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5