2013-05-01 33 views
1

我有這樣的腳本:在jQuery的1.9我的圖像互換不工作

jQuery(document).ready(function(){ 
    $(".imgSwap").mouseenter(function() { 
    $(this).src("_off").replaceWith("_on"); 
    }).mouseleave(function() { 
    $(this).src("_on").replaceWith("_off"); 
    }); 
}); 

而且這個網站:

<div id="nav"> 
    <table> 
    <tr> 
     <td class="tdleft"> 
     <img src="../../nav/prev_off.png" width="104" height="37" /></td> 
     <td class="tdcenter"> 
     <img src="../../nav/crumb_on.png" width="30" height="30" alt="Pagina 1" /> 
     <img src="../../nav/crumb_off.png" width="30" height="30" /> 
     <img src="../../nav/crumb_off.png" width="30" height="30" /></td> 
     <td class="tdright"> 
     <a href="p02.html"><img class="imgSwap" src="../../nav/next_off.png" width="104" height="37" /></a></td> 
    </tr> 
    </table> 
</div> 

我無法說服我帶班imgSwap圖像交換上的mouseenter或鼠標離開。 我覺得問題出在代碼replaceWith-part。 但我不知道如何解決它,並且我無法找到一個清晰的例子(至少在我看來)在Stack Overflow上。

+0

圖像置換等,這是一個有點討厭,你要考慮的精靈或使用顯示和隱藏。 – Blowsie 2013-05-01 14:07:19

回答

2

而不是

$(this).src("_off").replaceWith("_on"); 

使用

this.src = this.src.replace("_off", "_on"); 

你想要做的src字符串替換而replaceWith做DOM元素的替代品。

你也應該使用hover可能簡化代碼:

$(function(){ 
    $(".imgSwap").hover(function() { 
     this.src = this.src.replace("_off", "_on"); 
    }, function(){ 
     this.src = this.src.replace("_on", "_off"); 
    }); 
}); 
+0

感謝您的回覆。我試過這個,但它仍然無法正常工作。此外,我讀hover()不應該用於新版1.9版本中的mouseenter,mouseleave。 – silvith 2013-05-01 10:19:50

+0

我不明白會出現什麼問題。你可以建立一個[小提琴](http://jsbin.com)? – 2013-05-01 10:28:11

+0

好吧,我再試一次,現在它工作。我不知道爲什麼它一開始不起作用,但它現在確實如此。謝謝! :) – silvith 2013-05-01 13:18:50