图片切换

点击图片,对图片进行切换

 1 <!DOCTYPE HTML>
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 5 <title>无标题文档</title>
 6 
 7 </head>
 8 
 9 <body>
10 
11 <img id="img1" src="img/2.jpg" width="200" />
12 
13 <script>
14 var oImg = document.getElementById('img1');
15 var onOff = true;        // 布尔值:true 1  false 0
16 
17 oImg.onclick = function (){
18     // if( oImg.src == 'img/2.jpg' ){
19     // 有条件,就用现成的,如果没有,创造条件也得做事
20         
21     if( onOff ){
22         oImg.src = 'img/4.jpg';
23         onOff = false;
24     } else {
25         oImg.src = 'img/2.jpg';
26         onOff = true;
27     }
28 };
29 
30 </script>
31 
32 </body>
33 </html>
示例代码

 

posted @ 2017-05-23 22:23  #安生  阅读(117)  评论(0编辑  收藏  举报