繁体   English   中英

按下按钮更改图像

[英]Change image with a press on the button

我想通过使用按钮将我的图像更改为gif,这部分代码我已经工作了。
但是现在他们让我用第二个按钮将gif改回到上一个图像(我们称之为停止按钮)。

这是我到目前为止的代码:

<script>
function pictureChange()
{
document.getElementById("theImage").src="http://www.animaties.com/data/media/194/vis-bewegende-animatie-0135.gif";
}
</script>
<body>
<img id="theImage" src="http://www.pastasite.nl/file/2011/11/vis.jpg">
<p><input type="button" id="theButton" value="click me!" onclick="pictureChange()"></p>
</body>

这些图像仅作为示例。

只要else条件

 function pictureChange() { if (document.getElementById("theImage").src == "http://www.pastasite.nl/file/2011/11/vis.jpg") { document.getElementById("theImage").src = "http://www.animaties.com/data/media/194/vis-bewegende-animatie-0135.gif"; } else { document.getElementById("theImage").src = "http://www.pastasite.nl/file/2011/11/vis.jpg"; } } 
 <img id="theImage" src="http://www.pastasite.nl/file/2011/11/vis.jpg"> <p> <input type="button" id="theButton" value="click me!" onclick="pictureChange()"> </p> 

在更改src之前,将原始src保存在数据属性中

 function pictureChange() { var elem = document.getElementById("theImage"); if (!elem.getAttribute("data-origsrc")) { elem.setAttribute("data-origsrc", elem.getAttribute('src')); } elem.src = "http://www.animaties.com/data/media/194/vis-bewegende-animatie-0135.gif"; } function revert() { var elem = document.getElementById("theImage"); elem.src = elem.getAttribute("data-origsrc"); } 
 <img id="theImage" src="http://www.pastasite.nl/file/2011/11/vis.jpg"> <p> <input type="button" id="theButton" value="click me!" onclick="pictureChange()"> <input type="button" value="stop" onclick="revert()"> </p> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM