简体   繁体   English

'jQuery.each(lines,function(lineNo,line)')是做什么的?

[英]What does 'jQuery.each(lines, function(lineNo, line)' do?

I'm a beginner in jquery and ajax. 我是jquery和ajax的初学者。 While i was going through some example online, i came across the following piece of code and wondered what exactly it does. 当我在线查看一些示例时,我遇到了以下代码,想知道它到底是做什么的。

    lines = newLine.split('#');

 jQuery.each(lines, function(lineNo, line) {                    
                        eval("linedata = " + line);             
                        data.push(linedata);

                    });

I'm not a programmer, but just trying to understand its functionality. 我不是程序员,而是试图了解其功能。 Can anyone help me? 谁能帮我?

The each function iterates over an array which is supplied as the first parameter. 每个函数在作为第一个参数提供的数组上进行迭代。 During each iteration the index and element are passed into a function that is performed. 在每次迭代期间,将索引和元素传递到执行的函数中。 The function is passed as the second parameter to the each function. 该函数作为第二个参数传递给each函数。

Read more on the jQuery Documentation 阅读有关jQuery文档的更多信息

In the example you have provided a string newLine is split into an array using # as the delimiter. 在该示例中,您提供了一个字符串,将newLine使用#作为分隔符分割为一个数组。

The each function then iterates over the newly created array, assigning the value of each element to a variable linedata and pushes linedata onto another array. each函数然后遍历新创建的数组,将每个元素的值分配给变量linedata并将linedata推入另一个数组。

This could be more easily achieved with the following, since the call to eval is unnecessary: 由于不需要调用eval,因此可以通过以下方法更轻松地实现:

 jQuery.each(lines, function(lineNo, line) {                                
     data.push(line);
 });

I pretended, for a moment, that I was a new programmer. 我假装我是一个新程序员。 This is how you should go about looking into things from here on out: 这是您应该从头开始研究的方法:

1.) Ok, I don't know what this first line is doing. 1.)好吧,我不知道第一行在做什么。 It's splitting something (based on the split word). 它正在拆分某些内容(基于split单词)。 Hmmm let's Google for "split javascript". 嗯,让我们用Google来了解“拆分javascript”。 This is the first thing that comes up. 这是第一件事。 From here, you may be wondering what a String is, so you would search for that as well). 从这里开始,您可能想知道什么是字符串,因此也要搜索它。

2.) Ok so now I know that splitting a String gives me an array (again you probably looked this up by this step) of the newLine substrings that were separated by the # character. 2.)好吧,现在我知道拆分字符串给了我一个由#字符分隔的newLine子字符串数组(同样,您可能在此步骤中对此进行了查找)。 Cool. 凉。 So let's look into what jQuery.each does. 因此,让我们研究一下jQuery.each功能。 I google "jQuery.each" and this is the first thing that comes up . 我用谷歌搜索“ jQuery.each”, 这是第一件事

Awesome! 太棒了! Now you understand what a String is, an Array , the split function from String as well as what jQuery.each is. 现在您了解了什么是StringArrayStringsplit函数以及jQuery.each是什么。 :D :D

EDIT: As you move forward, you'll realize that W3C is generally an inferior source of information. 编辑:继续前进,您将意识到W3C通常是劣等的信息来源。 I simply linked to it since it was literally the first thing that came up when I Googled "split javascript". 我只是链接到它,因为当我在Google中搜索“拆分javascript”时,它实际上是第一件事。 Overall it does the job for giving you a good overview of certain things when you're learning them for the first time. 总的来说,它可以帮助您在初次学习某些东西时对它们进行很好的概述。

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

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