image onload仅更改第一个URL

时间:2010-01-21 04:32:55

标签: jquery

我正在使用overlay + gallery插件。但我一度陷入困境。当前代码用新的替换#main div中的所有URL地址(wrap.find(“a”)。attr(“href”,url);)。但我希望它只替换第一个。请指教。我希望这个问题很清楚。

        <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();

1 个答案:

答案 0 :(得分:0)

变化:

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

要:

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

或者您也可以这样做:

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