JavaScript-Newcomers to ask a simple question

Source: Internet
Author: User
Tags jquery library

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

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.