2012-08-10 71 views
0

我想要它,所以,如果你選擇的id'defaultone'或'defaulttwo'的圖像之一,那麼具有id'actualone'的圖像更改爲該圖像。我知道我非常接近,但是我有一個小小的錯誤。有人可以幫幫我嗎??Javascript onclick改變id img

 <script type="text/javascript"> 
     // Popup window code 
     function newPopup(url) { 
      popupWindow = window.open(url,'popUpWindow','height=450,width=600,left=10,top=10,resizable=no,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes') 
     } 
     function bigimg(x) { 
      x.style.height="65px"; 
      x.style.width="85px"; 
      x.style.opacity="0.5"; 
     } 
     function defaultimg(x) { 
      x.style.height="60px"; 
      x.style.width="80px"; 
     } 
     function teamback(x) { 
      document.getElementById("x").src = document.getElementById("defaultone").src; 
     } 
     </script> 

    </head> 
     <body> 
      Your Team <img id="defaultone" onmouseover="bigimg(this)" onclick="teamback(this)" onmouseout="defaultimg(this)" src="cowboys.gif"> vs <img id="defaulttwo" onmouseover="bigimg(this)" onclick="teamback(this)" onmouseout="defaultimg(this)" src="giants.gif"><img src="" id="actualone" style="width:85px; height:65px;"><br><br> 
      <img src="colts.gif"> vs <img src="bears.gif"> 
     </body> 
</html> 
+0

我在代碼中看不到x元素。 – 2012-08-10 22:52:25

+0

x元素是'this' – 2012-08-10 22:53:58

+1

然後你不應該有雙引號,x.id – 2012-08-10 22:55:04

回答

3

在你的JavaScript方法teamover(),你被錯誤地引用與document.getElementById("x")搬出元素;元素"x"不存在。

嘗試更新到這一點:

function teamback(x) { 
    // update the "actualone" image's source to the sending-image's source 
    document.getElementById("actualone").src = x.src; 
} 
+0

lmao newfurniturey拉:P – effectica 2012-08-10 23:01:16

2

使用jQuery(您已經包含在HTML文檔的頭部):

<script type="text/javascript"> 
    $(document).ready(function(){ 
     // For both 
     $('#defaultone, #defaulttwo').click(function(){ 
      $('#actualone').attr('src', $(this).attr('src')); 
     }); 
    }); 
</script> 

改寫爲使用一個功能。

+0

你可以想出一種方式,使它只有這兩個內部功能之一...就像上面的答案,但在jQuery .. – 2012-08-10 23:01:47

+1

嗯...給我一分鐘,我會重寫它 – Grenville 2012-08-10 23:02:41

+1

你有希望這有助於 – Grenville 2012-08-10 23:09:48

0

它應該是如下

function teamback(x) { 
    x.src = document.getElementById("defaultone").src; // To change the x element's src to defaultone's src 
} 

由於您使用this不是它的ID傳遞元素本身。

0

它看起來像你想在這一行訪問x

document.getElementById("x").src = 

它應該是:

x.src = 

此外,在onclick碼等之後它應該有一個分號,就像

onclick="teamback(this);"