因为浏览器会缓存图片,所以对于一次性gif如果不做处理的话,只会播放一次。如果需要重新播放gif,重新设置下img标签的src属性即可。
<!doctype html><html><head> <meta charset="UTF-8" /> <title>测试</title> <script type="text/javascript"> window.onload = function() { document.getElementById('div1').addEventListener('click', function() { document.getElementById('img1').src = document.getElementById('img1').src }); } </script></head><body> <div id="div1" style="width: 100px; height: 100px;background-color:#eeffee;"> 点击测试</div> <img id="img1" src="https://img-blog.csdnimg.cn/20190616203502503.gif"></img></body></html>