2012-10-24 85 views
0

我一直有這個問題。我認爲這應該是非常簡單的,但我似乎無法得到它的工作。我想在滾動我的Facebook按鈕時出現一個新圖片。謝謝你的幫助!jquery更改鼠標滾動圖像

<p align="right"> 
      <a href="http://www.facebook.com/AerialistPress" ><img height="30px" id="facebook" class="changePad" alt="Aerialist Press Facebook Page" src="/sites/aerialist.localhost/files/images/facebook300.jpg" /></a> 
      <a href="http://twitter.com/#!/AerialistPress" > <img height="30px" class="changePad" alt="Aerialist Press Twitter Page" src="/sites/aerialist.localhost/files/images/twitter300.jpg" /></a> 
      <a href="http://www.pinterest.com/aerialistpress" ><img height="30px" class="changePad" alt="Aerialist Press Pinterest Page" src="/sites/aerialist.localhost/files/images/pinterest300.jpg" /></a> 
</p> 

<script> 
jQuery(document).ready(function(){ 
    jQuery('#facebook').mouseover(function() { jQuery('#facebook').attr('src').replace('/sites/aerialist.localhost/files/images/facebook-roll.jpg'); }) 

}); 
</script> 
+0

請檢查我的答案:) –

回答

1

attr方法返回指定屬性的值(在本例中爲'src'),並且replace正試圖修改該字符串並返回一個新實例。但是,您對新實例沒有做任何事情。

要設置該屬性,您必須將其他參數傳遞給attr方法。

這裏的文件爲attr方法: http://api.jquery.com/attr/

你的代碼應該是:

jQuery('#facebook').attr('src', '/sites/aerialist.localhost/files/images/facebook-roll.jpg'); 
0

不要使用replace,只需設置src ATTR直接:

jQuery('#facebook').attr('src', '/sites/aerialist.localhost/files/images/facebook-roll.jpg'); 
+0

謝謝!它會改變,但當我移動鼠標時,它保持不變。有沒有辦法讓它在老鼠身上變回來? –

0

變化

jQuery('#facebook').attr('src').replace('/sites/aerialist.localhost/files/images/facebook-roll.jpg'); 

jQuery('#facebook').attr('src', '/sites/aerialist.localhost/files/images/facebook-roll.jpg'); 

您還可以使用$('#facebook')而不是$('#facebook')

+0

謝謝!它會改變,但當我移動鼠標時,它保持不變。有沒有辦法讓它在老鼠身上變回來? –

+0

是的,使用'hover()' – Grzegorz

+0

http://stackoverflow.com/questions/5589382/jquery-changing-the-src-of-another-image-on-rollover – Grzegorz

0

這裏亞去!

$("#img").hover(function(){ 
//mouseover 
    $(this).attr('src', 'https://twimg0-a.akamaihd.net/profile_images/1768071248/smile_normal.jpg'); 
    }, 
    function(){ 
//mouseout 
    $(this).attr('src', 'http://www.bestfreeicons.com/smimages/Emotes-face-smile-icon.png'); 
});​ 
0

這工作

<script type ="text/javascript"> 

$(document).ready(function(){ 
$('#facebook').mouseenter(function() { 
    $('#facebook').attr("src","heretheotherimage.png"); 
}) 

$('#facebook').mouseleave(function() { 
    $('#facebook').attr("src","heretheimage.png"); 
}) 

}); 
</script> 

<img src ="heretheimage.png" id ="facebook" />