点击切换图片

方法1 用indexOf()判断

rg/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script>
 window.onload = function(){
  var oImg = document.getElementById('img1');
  var onOff = true;
  
  oImg.onclick = function(){
   var pan = oImg.src.indexOf("girl") ;
   if (pan>=0) {
    oImg.src = '03.JPG';
   }else{
    oImg.src = "girl.JPG";
   }
  }
 };
</script>
</head>
 
<body>
    <img id="img1" src="./girl.JPG" alt="" />
</body>
</html>

 

方法2

 设置一个变量  var onOff = true;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script>
 window.onload = function(){
  var oImg = document.getElementById('img1');
  var onOff = true;
  oImg.onclick = function(){
   if (onOff) {
    oImg.src = '01.JPG';
    onOff = false;
   }else{
    oImg.src = '02.JPG';
    onOff = true;
   }
  };
 };
</script>
</head>
 
<body>
    <img id="img1" src="./01.JPG" alt="" />
</body>
</html>

posted @ 2014-12-01 16:25  人间最美二月天  阅读(170)  评论(0编辑  收藏  举报