2012-08-10 75 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);"