JS基础——两张图来回切换

来源:互联网 发布:javlibrary2017新域名 编辑:程序博客网 时间:2024/04/28 08:26


[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <!doctype html>  
  2. <html>  
  3. <head>  
  4. <meta charset="utf-8">  
  5. <title>JS基础——两张图来回切换</title>  
  6. <script>  
  7. window.onload = function(){  
  8.     var oImg = document.getElementById('img');  
  9.     var onOff = true;  
  10.       
  11.     oImg.onclick = function(){  
  12.         if(onOff){  
  13.             oImg.src = 'http://placehold.it/200x200';  
  14.             onOff = false;    
  15.         }else{  
  16.             oImg.src = 'http://placehold.it/150x150';  
  17.             onOff = true;     
  18.         }     
  19.     };  
  20.       
  21. };  
  22. </script>  
  23. </head>  
  24.   
  25. <body>  
  26. <img id="img" src="http://placehold.it/150x150">  
  27. </body>  
  28. </html>  

两张图片来回切换思路:

通过if else语句和自定义开关属性来判断图片切换

0 0
原创粉丝点击