2010-01-21 86 views
1

我正在處理overlay + gallery插件。但我陷入了一個困境。當前的代碼將用新的代碼(wrap.find(「a」)。attr(「href」,url);)替換#main div中的所有URL地址。但我希望它只取代第一個。請請諮詢。我希望問題清楚。image onload只改變第一個網址

 <div id="main"> 
      <a href="http://www.capalbosonline.com/images/634-large.jpg" title="Fairfax Gift Basket"><img src="http://www.capalbosonline.com/images/634-large.jpg" alt="Fairfax Gift Basket" id="productimage" /></a> 
      <a href="http://www.refinethetaste.com/html/media/images/2571243744_f1c6b487ff.jpg" title="Fairfax Gift Basket" ><img id="productimage" alt="Fairfax Gift Basket" src="http://www.refinethetaste.com/html/media/images/2571243744_f1c6b487ff.jpg"/></a>  
     </div> 


$(".productthumbs img").click(function() { 
    // calclulate large image's URL based on the thumbnail URL (flickr specific) 
    var url = $(this).attr("src"); 
    // get handle to element that wraps the image and make it semitransparent 
    var wrap = $("#main").fadeTo("medium", 0.5); 
    // the large image from flickr 
    var img = new Image(); 
    // call this function after it's loaded 
    img.onload = function() { 
     // make wrapper fully visible 
     wrap.fadeTo("fast", 1); 
     // change the image 
     wrap.find("img").attr("src", url); 
     wrap.find("a").attr("href", url); 
    }; 
    // begin loading the image from flickr 
    img.src = url; 
// when page loads simulate a "click" on the first image 
}).filter(":first").click(); 

回答

0

變化:

wrap.find("a").attr("href", url); 

要:

wrap.find("a").eq(1).attr("href", url); 

或者你也可以這樣做:

wrap.find("a:eq(0)").attr("href", url); 
+0

太感謝你了! – 2010-01-21 04:47:51