jQuery.each(function(index, value){}); 什么是价值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10968555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery.each(function(index, value){}); What is value?
提问by cadence441
I am learning jQuery from a book called Head First jQuery. The book is very easy to learn from. The point is, there is an .each()
function(included in image which I scanned from the ) which has a function() parameter. The function() parameters are index
and value
. The index is explained on the page, but what about the value? And also, since it is an anonymous function(which cannot be reused) how does it take any parameters?
我正在从一本名为Head First jQuery的书中学习jQuery。这本书非常容易学习。关键是,有一个.each()
函数(包含在我从 扫描的图像中)它有一个 function() 参数。function() 参数是index
和value
。页面上解释了索引,但是值呢?而且,由于它是一个匿名函数(不能重用),它如何接受任何参数?
回答by Sampson
There are two each
methods in jQuery. One is for cycling over a jQuery object which contains many matches. For instance, suppose we wanted to find all paragraphs on the page:
each
jQuery 中有两种方法。一种是循环遍历包含许多匹配项的 jQuery 对象。例如,假设我们要查找页面上的所有段落:
$("p").each(function(){
// Do something with each paragraph
});
Secondly, there is a more generic each
for iterating over objects or arrays:
其次,有一个更通用 each
的迭代对象或数组:
var names = ["Jonathan", "Sampson"];
$.each(names, function(){
// Do something with each name
});
When jQuery cycles over the elements in either of these examples, it keeps count of which object it's currently handling. When it executes our anonymous function, it passes in two parameters - the current value we're on (index), and that object (value).
当 jQuery 循环遍历这些示例中的任何一个中的元素时,它会计算当前正在处理的对象。当它执行我们的匿名函数时,它传入两个参数 - 我们所在的当前值(索引)和该对象(值)。
var names = ["Jonathan", "Sampson"];
$.each(names, function(index, value){
alert( value + " is " + index );
});
Which outputs "Jonathan is 0", and "Sampson is 1" since we're using a zero-based index.
由于我们使用的是从零开始的索引,因此输出“Jonathan is 0”和“Sampson is 1”。
But what about our native jQuery object?
但是我们的原生 jQuery 对象呢?
$("p").each(function(index, value){
alert( value.textContent ); // The text from within the paragraph
});
In this case, value
is an actual HTMLParagraphElement
object, so we can access properties like textContent
or innerText
on it if we like:
在这种情况下,value
是一个实际的HTMLParagraphElement
对象,所以我们可以访问像textContent
或innerText
在它上面的属性,如果我们愿意:
回答by Preli
This second paremter which you called value is the valueof the collection that is currently processed by the each function.
您称为 value 的第二个参数是每个函数当前处理的集合的值。
For your second question - anonymous functions can be reused, just because they don't have a name doesn't mean they can't have parameters or be executed. See following example:
对于您的第二个问题 - 匿名函数可以重用,仅仅因为它们没有名称并不意味着它们不能有参数或被执行。请参阅以下示例:
function execute_fn(fn) {
fn(1,2);
}
execute_fn(function(a,b) { ... });