点击图片,对图片进行切换
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>
示例代码
时间: 2024-10-19 11:51:24