2015-06-22 105 views
0

我遇到了onmouseover命令的問題。我在我的HTML代碼得到的代碼(參見下圖)這一點,當我嘗試在Safari 7我收到以下錯誤運行:TypeError:undefined不是對象

TypeError: undefined is not an object (evaluating 'document.images.siz3.src='images/shirts/img_3siz_grey.jpg'') onmouseover

什麼問題?我找不到任何可能是錯誤的。 img應該定義爲siz1,不應該嗎?

也許有人可以幫忙,我敢肯定答案很簡單,我只是沒有看到它。

<a href="menu/latein/r-z/vfc/vfc.html" onmouseover="document.images.siz1.src='images/shirts/img_1siz_grey.jpg'" onmouseout="document.images.siz1.src='images/shirts/img_1_siz.jpg'" name="tshirts"> 
 
<img src="images/shirts/img_1_siz.jpg" name="siz1「 alt="Vivat floreat crescat" width="195px" height="135px"></img>

+2

最好添加事件在JavaScript中。內聯事件是不好的做法。看看'addEventListener'。另外,請注意引號'「!=」' – elclanrs

+0

檢查'img'標籤的名稱屬性,它沒有被正確的聲明,我也在問題中編輯了它,如果這是問題,你可以再試一次嗎? –

+0

啊,謝謝!這是引號...... –

回答

1

看起來問題在這裏name="siz1「應該name="siz1"取代"

+1

[正如elclanrs說](http://stackoverflow.com/questions/30977161/typeerror-undefined-is-not-an-object#comment49984847_30977161) - 當張貼其他人的評論作爲答案時,它最好把它變成CW。 –

+0

@ T.J。 Crowder我沒有看到評論 – ozil

0

你沒有用正確的引號關閉 '名稱' 標籤。

<a href="menu/latein/r-z/vfc/vfc.html" onmouseover="document.images.siz1.src='images/shirts/img_1siz_grey.jpg'" onmouseout="document.images.siz1.src='images/shirts/img_1_siz.jpg'" name="tshirts"> 
 
<img src="images/shirts/img_1_siz.jpg" name="siz1" alt="Vivat floreat crescat" width="195px" height="135px"></img>

0

試試這個代碼

<a href="#" onmouseover="change('change')" onmouseout="change('')">Click</a> 
<img id="siz1" src="images/shirts/img_1_siz.jpg" height="200" width="200" /> 
<script> 
    function change(str) { 
     if (str == "") { 
      document.getElementById('r1').src = "images/shirts/img_1_siz.jpg"; 
     } 
     else { 
      document.getElementById('r1').src = "images/shirts/img_1siz_grey.jpg"; 
     } 
    } 
</script> 
相關問題