Javascript 如何在jQuery each()中传递1个以上的参数?

Javascript 如何在jQuery each()中传递1个以上的参数?,javascript,jquery,arguments,each,Javascript,Jquery,Arguments,Each,如何将索引和元素以外的内容传递给jQuery的每个函数? 我正在尝试归档以下内容: function HelloWorld() { this.foo = function(obj) { console.log(obj); } this.test = function(className) { var that = this; // I need HelloWorld inside $("."+className).each(function

如何将索引和元素以外的内容传递给jQuery的每个函数? 我正在尝试归档以下内容:

function HelloWorld() {
   this.foo = function(obj) {
      console.log(obj);
   }

   this.test = function(className) {
      var that = this; // I need HelloWorld inside
      $("."+className).each(function(hw) {
          hw.foo(this);
      }(that));
   }
}
      var that = this;
      $("."+className).each(function(index, elm) {
          // `this` here is element of className
          // `elm` here is the same as this
          // `that` here is what `this` was in higher closure
          // `index` here is the number of the element in a sequence

      });
然后在页面的某个地方,假设我有十几个元素,它们都有一个特定的类名

例:

但是,当我在each函数中时,this成为整个页面的全局this,我需要它是来自$.+className的jQuery元素的this

如果我没有在匿名函数的末尾传递它,那么这将是$.+className.

中的元素,它位于更高的范围内,因此它已经在每个循环中可用:

function HelloWorld() {
   this.foo = function(obj) {
      console.log(obj);
   }

   this.test = function(className) {
      var that = this;
      $("."+className).each(function(index, elem) {
          that.foo(elem);
      });
   }
}
这在更高的范围内,因此它在每个循环中都已可用:

function HelloWorld() {
   this.foo = function(obj) {
      console.log(obj);
   }

   this.test = function(className) {
      var that = this;
      $("."+className).each(function(index, elem) {
          that.foo(elem);
      });
   }
}

这里有一个错误:

      var that = this;
      $("."+className).each(function(hw) {
          hw.foo(this);
      }(that));
这部分不好:

      }(that));
来自更高闭包的所有变量都已经可用,您不需要以任何方式传递它

应该是这样的:

function HelloWorld() {
   this.foo = function(obj) {
      console.log(obj);
   }

   this.test = function(className) {
      var that = this; // I need HelloWorld inside
      $("."+className).each(function(hw) {
          hw.foo(this);
      }(that));
   }
}
      var that = this;
      $("."+className).each(function(index, elm) {
          // `this` here is element of className
          // `elm` here is the same as this
          // `that` here is what `this` was in higher closure
          // `index` here is the number of the element in a sequence

      });

这里有一个错误:

      var that = this;
      $("."+className).each(function(hw) {
          hw.foo(this);
      }(that));
这部分不好:

      }(that));
来自更高闭包的所有变量都已经可用,您不需要以任何方式传递它

应该是这样的:

function HelloWorld() {
   this.foo = function(obj) {
      console.log(obj);
   }

   this.test = function(className) {
      var that = this; // I need HelloWorld inside
      $("."+className).each(function(hw) {
          hw.foo(this);
      }(that));
   }
}
      var that = this;
      $("."+className).each(function(index, elm) {
          // `this` here is element of className
          // `elm` here is the same as this
          // `that` here is what `this` was in higher closure
          // `index` here is the number of the element in a sequence

      });

您没有真正正确地执行此操作,是什么让您认为可以将foo链接到DOM元素?请注意DIV不是一个自动关闭的元素?您没有真正正确地执行此操作,是什么让您认为可以将foo链接到DOM元素?请注意DIV不是一个自动关闭的元素?