2016-11-05 120 views
-1

鼠標懸停功能,我有最簡單的代碼,但之前的onmouseover用我從來沒有嘗試過,所以不知道爲什麼它不工作:爲什麼不工作

<div class="play" id="buttonPlay"> 
    <img src="Buttons/Play/playRest.png" onmouseover="this.src='Buttons/Play/playClick.png'" width="100%"> 
</div> 

任何想法?有什麼好的方法去調試它?

+0

我複製代碼替換爲'alert()'並且警報顯示,所以它應該正在運行。 –

+0

[打開瀏覽器的JS控制檯](http://webmasters.stackexchange.com/questions/8525/how-to-open-the-javascript-console-in-different-browsers)並查看錯誤消息是什麼。 – JJJ

+0

定義「不工作」。圖像是否被破碎的圖像取代?控制檯中是否顯示錯誤消息?什麼都沒有發生? – Quentin

回答

-2

嘗試在腳本元素中聲明您的函數並引用該函數,而不是全部內聯。至少你可以在瀏覽器的開發者控制檯中設置Breakpoint,看看發生了什麼。

<div class="play" id="buttonPlay"> 
    <img src="Buttons/Play/playRest.png" onmouseover="myFunction(this)" width="100%"> 
</div> 
<script> 
    function myFunction(element) { 
     element.src='Buttons/Play/playClick.png'; 
    } 
</script> 
+0

這是行不通的。 'this'現在是'窗口' – Quentin

+0

重新編輯:現在你沒有打破它,但你似乎沒有解決這個問題。你只是建議一條調試線。 (通過在'onmouseover'屬性值前面加上'debugger'來設置斷點會更容易) – Quentin

+0

@Quentin你是對的,我編輯了它,但它不是一個解決方案,它只是一種方便調試的方法。 –

-2

嘗試使用setAttribute來代替,當我這樣做的時候,這個工作。

<div class="play" id="buttonPlay"> 
    <img src="Buttons/Play/playRest.png" onmouseover="this.setAttribute('src', 'Buttons/Play/playClick.png');" width="100%"> 
</div> 
+1

爲什麼'src'屬性不工作? – Quentin