This is a heading
This is a paragraph.
This is another paragraph.
在每个 p 元素的结尾添加内容
$("p").append(function(n){ return "This p element has index " + n + "";
What is the meaning of n in the code above, is it a direct definition of a variable or something?
Reply content:
This is a heading
This is a paragraph.
This is another paragraph.
在每个 p 元素的结尾添加内容
$("p").append(function(n){ return "This p element has index " + n + "";
What is the meaning of n in the code above, is it a direct definition of a variable or something?
$.append(function(n){ });
The N of this method you can understand as index, starting from 0, see Jqueryapi Append
Please refer to: Description of the Append method in JQuery
Let me answer this question, this is the question of the callback function, the specific use of the callback function please refer to here: Link description, there is my answer to the callback function
First append is a method that this method belongs to the $ (' P ') object and then you $ (' P '). Append (pass in a parameter);
You are here to pass a function to append as the parameter used by append, and then append this method to use your callback function, and this value is append this method is passed to the callback function.
n is the function parameter, which is passed when the jquery library calls your callback function.
An anonymous function is passed in the Append method, and n is just a local variable
n is a parameter that can be received in callback when the Append method is defined.
$ ("P"). Append (function (n) {
return "This p element has index " + n + ""; 这里选择标签P,append就是在字符串后面增加的意思,里面的function返回的是This p element has index " + n + ",而这个返回就是当做了append()方法里的参数,也就是在“p”标签后面加上This p element has index " + n + " 如This is a paragraph.
这句就变成了This is a paragraph.This p element has index " + n + "
当然这个n也是一个传入的参数,具体看函数的给定
n This parameter write index, or will make you more clear, understand, in fact, this n refers to the position in the matching set (index position), in fact, in the official website to check the document can be
First, that is a callback function, and then this n is a parameter to the callback function.
n is not a variable but a function parameter, a special variable that belongs to this function object