jQuery - this.option在加载函数中变为未定义

时间:2013-05-14 11:23:44

标签: javascript jquery load this

我正在尝试稍微更改插件,这是原型功能,我已经标记了我添加的行。

好的,所以我的问题发生在我添加的img-load函数中。我用一个代码包围旧代码,以确保脚本等待图像加载。 问题是,负载功能中的“this”与外部的“this”没有关联。我试过给load函数一个参数,但显然它不起作用或者我做错了。

你知道一种简单的方法来继承“这个”吗?我真的不知道还能做什么。

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 个答案:

答案 0 :(得分:1)

你在闭包变量“_this”中捕获了这个。这不起作用吗?