2013-05-14 15 views
0

我試圖改變一個插件一點,這裏是Prototype函數,我已經標記了我添加的行。jQuery - this.option在加載函數中變得未定義

好吧,所以我的問題發生在我添加的img-load函數中。我用一個代碼包圍了舊代碼,以確保腳本等待圖像加載完成。 問題是,負載功能中的「this」與外部的「this」沒有連接。我已經嘗試給加載函數一個參數,但顯然它不工作,或者我做錯了什麼。

你知道一個簡單的方法來排序 - 繼承「this」嗎?我真的不知道還有什麼要做。

Plugin.prototype._fade = function(number) { 
    var $element, currentSlide, next, slidesControl, value, 
    _this = this; 
    $element = $(this.element); 
    this.data = $.data(this); 
    if (!this.data.animating && number !== this.data.current + 1) { 
    $.data(this, "animating", true); 
    currentSlide = this.data.current; 
    if (number) { 
     number = number - 1; 
     value = number > currentSlide ? 1 : -1; 
     next = number; 
    } else { 
     value = this.data.direction === "next" ? 1 : -1; 
     next = currentSlide + value; 
    } 
    if (next === -1) { 
     next = this.data.total - 1; 
    } 
    if (next === this.data.total) { 
     next = 0; 
    } 

    this._setActive(next); 
    slidesControl = $(".slidesjs-control", $element); 



    var nxtImg = $(slidesControl.children(":eq(" + next + ")")).find("img:eq(0)"); // added 
    if (nxtImg.attr("longdesc") !== undefined) { // added 
     nxtImg.attr("src", nxtImg.attr("longdesc")); // added 
     nxtImg.removeAttr("longdesc"); // added 
    } // added 


    nxtImg.load(function(){ // added 
     slidesControl.children(":eq(" + next + ")").css({ 
     display: "block", 
     left: 0, 
     zIndex: 0 
     }); 
     this.options.callback.start(currentSlide + 1); 
     if (this.options.effect.fade.crossfade) { 
     return slidesControl.children(":eq(" + this.data.current + ")").stop().fadeOut(this.options.effect.fade.speed, (function() { 
      slidesControl.children(":eq(" + next + ")").css({ 
      zIndex: 10 
      }); 
      $.data(_this, "animating", false); 
      $.data(_this, "current", next); 
      return _this.options.callback.complete(next + 1); 
     })); 
     } else { 
     slidesControl.children(":eq(" + next + ")").css({ 
      display: "none" 
     }); 
     return slidesControl.children(":eq(" + currentSlide + ")").stop().fadeOut(this.options.effect.fade.speed, (function() { 
      slidesControl.children(":eq(" + next + ")").stop().fadeIn(_this.options.effect.fade.speed).css({ 
      zIndex: 10 
      }); 
      $.data(_this, "animating", false); 
      $.data(_this, "current", next); 
      return _this.options.callback.complete(next + 1); 
     })); 
     } 
    }); // added 



    } 
}; 

回答

1

你在一個閉包變量「_this」中捕獲了這個。這不正常嗎?

+0

其實你是對的!我只是將此與這個交換,現在一切正常。那麼簡單,但我無法自己弄清楚。謝謝! – user828591 2013-05-14 15:08:38