<script type="text/javascript">
function changeSrc(){ if(document.getElementById("pic").src=="images/5.jpg") { document.getElementById("pic").src="images/6.jpg"; } if(document.getElementById("pic").src="images/6.jpg") { document.getElementById("pic").src="images/5.jpg"; } } </script>
<img id="pic" align="center" margin-right="10px" src="images/6.jpg" width="25%" οnclick="return changeSrc()"/>
点击图像只能改变一次图像显示,想要改变多次则把代码改写为其他的
<script type="text/javascript">
function changeSrc(){ var element=document.getElementById("pic"); if(element.src.match("images/5.jpg")) { element.src="images/6.jpg"; }else{ element.src="images/5.jpg"; } } </script>
<img id="pic" align="center" margin-right="10px" src="images/6.jpg" width="25%" οnclick="return changeSrc()"/>
再次进行优化,当有多个图片进行切换时,怎么确定是那个图片,如下可以这么做
<script type="text/javascript">
function changeSrc(dom){ var myid=dom.getAttribute("id"); var element=document.getElementById(myid); if(element.src.match("images/5.jpg")) { element.src="images/6.jpg"; }else{ element.src="images/5.jpg"; } } </script>
<img id="pic11" align="center" margin-right="10px" src="images/6.jpg" width="25%" οnclick="changeSrc(this)"/>
<img id="pic12" align="center" margin-right="10px" src="images/6.jpg" width="25%" οnclick="changeSrc(this)" />