2011-12-12 17 views
2

我試圖在現有的幻燈片功能中對鼠標懸停進行圖像交換。幻燈片功能要求我爲數組添加值。在那個數組中,我想打電話給我的onMouseOver事件處理程序,但是我相信我遇到了可疑字符的問題。在數組值中轉義字符

我嘗試:

var leftrightslide=new Array() 
leftrightslide[0]='<a href="#" onMouseOver="copyspeed=0; document.blah.src=\'images/slides/slideshow_top_01_mo.gif\'" onMouseout="copyspeed=slidespeed"><img src="images/slides/slideshow_top_01.gif" name=\'blah\'></a>'; 

在我的鼠標懸停,什麼都沒發生,但是當我嘗試在短短的HTML複製這一點,它工作正常。這是什麼使我相信我沒有正確處理撇號在我的事件處理程序:

<a href="" onMouseOver="document.blah2.src='images/slides/slideshow_top_01_mo.gif';" onMouseout="document.blah2.src='images/slides/slideshow_top_01.gif'"><img src="images/slides/slideshow_top_01.gif" name='blah2'></a> 

編輯:Here is a link to a test page with full javascript.

+1

第一個工作得很好,我在Chrome:http://jsfiddle.net/BEgRD/ –

+0

感謝您的答覆阿曼恩。爲了方便,我添加了一個鏈接到頁面。 – etm124

+0

btw,用'[]'代替'new Array()' – hugomg

回答

1

在我看來,你的腳本版本是失敗,因爲blah不是唯一的。每個實例都需要一個唯一的參考名稱。

或者,通過this以及鼠標操作來識別目標。

編輯

事情是這樣的:

<script> 
    function over(a){ 
     a.firstChild.src="bbb.jpg"; 
    } 
    function out(a){ 
     a.firstChild.src="aaa.jpg"; 
    } 
</script> 
<a onmouseover="over(this)" onmouseout="out(this)"><img src="aaa.jpg"/></a> 
<a onmouseover="over(this)" onmouseout="out(this)"><img src="aaa.jpg"/></a> 
+0

你有關於如何在這種情況下使名稱獨一無二的建議? – etm124