简体   繁体   English

jQuery需要帮助的理解(i,el)

[英]JQuery-Need help understanding (i, el)

I am having trouble understanding this piece of code. 我在理解这段代码时遇到了麻烦。

var win = $(window);
var allMods = $(".module");

// Already visible modules
allMods.each(function(i, el) {
  var el = $(el);
  if (el.visible(true)) {
    el.addClass("already-visible"); 
  } 
});

win.scroll(function(event) {

  allMods.each(function(i, el) {
    var el = $(el);
    if (el.visible(true)) {
      el.addClass("come-in"); 
    } 
  });

});

I understand that the bottom half of the code runs when the window is scrolled and that both allMods.each blocks are loops that iterate over all the elements with the "module" class. 我了解到,当滚动窗口时,代码的下半部分将运行,并且allMods.each块都是循环,这些循环使用“模块”类遍历所有元素。

There are three things I don't understand: 我不明白三件事:

  1. Shouldn't the .each function take in an array or object and a callback function as parameters? .each函数是否不应该将数组或对象以及回调函数作为参数? Here it just takes a function. 在这里,它只是一个功能。
  2. Why are "i" and "el" being passed in as parameters to the function. 为什么将“ i”和“ el”作为参数传递给函数。 I'm guessing "el" refers to the current element being iterated over, but why did they call it "el". 我猜“ el”是指当前元素被迭代,但是为什么他们称其为“ el”。 Could you replace all the "el"s for any other name? 您可以将所有“ el”替换为其他名称吗? I have no idea what "i" is. 我不知道“ i”是什么。
  3. When does the block in the top half run? 上半部分的代码何时开始运行? It is not inside the win.scroll event. 它不在win.scroll事件中。

Shouldn't the .each function take in an array or object and a callback function as parameters? .each函数是否不应该将数组或对象以及回调函数作为参数? Here it just takes a function. 在这里,它只是一个功能。

No. You are calling it as a method on a jQuery object. 不。您将其作为jQuery对象的方法调用。 It is that object that it loops over. 它就是那个循环的对象。 Internally each uses this to determine that. 内部eachthis来确定。

Why are "i" and "el" being passed in as parameters to the function. 为什么将“ i”和“ el”作为参数传递给函数。 I'm guessing "el" refers to the current element being iterated over, but why did they call it "el". 我猜“ el”是指当前元素被迭代,但是为什么他们称其为“ el”。 Could you replace all the "el"s for any other name? 您可以将所有“ el”替换为其他名称吗? I have no idea what "i" is. 我不知道“ i”是什么。

You can use whatever names you like for function arguments, it has no effect on what values are passed into it when it is called. 您可以为函数参数使用任何您喜欢的名称,它对调用它时传递给它的值没有影响。 Determining that is the responsibility of the calling function (ie the internals of each ). 确定这是调用函数的责任(即each函数的内部)。 The values that are passed into them are described in the documentation for each . each文档的文档中都描述了传递给它们的值。

Type: Function( Integer index, Element element )

The first argument is the index in the jQuery object. 第一个参数是jQuery对象中的索引。 The second argument is the element that is the value of that index. 第二个参数是该索引值的元素。

When does the block in the top half run? 上半部分的代码何时开始运行? It is not inside the win.scroll event. 它不在win.scroll事件中。

The same time any other code that isn't inside a function will run. 同时,不在函数内的任何其他代码也将运行。 When the script is loaded into the document. 脚本加载到文档中时。

  1. No. $().each() runs on a jQuery object (collection of elements) and only takes a function as a parameter. 否。 $().each()在jQuery对象(元素集合$().each()运行,并且仅将函数作为参数。
  2. See the documentation linked above. 请参阅上面链接的文档。 i is the index (0-based) and el is the element itself. i是索引(从0开始),而el是元素本身。 Yes, you can freely change the names as long as you change the references inside the function as well. 是的,您也可以自由更改名称,只要您也更改函数内部的引用即可。
  3. It runs at startup, whenever that block of code gets executed. 每当执行该代码块时,它就会在启动时运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM