Home  >  Article  >  Web Front-end  >  Analyze the two uses of after in jQuery

Analyze the two uses of after in jQuery

巴扎黑
巴扎黑Original
2017-06-24 11:30:141968browse

Definition and usage

after() method inserts the specified content after the selected element.

Syntax

$(selector).after(content)

Parameter Description

content Required. Specifies the content to be inserted (can include HTML tags).

Use the function to insert content

Use the function to insert the specified content after the selected element.

Syntax

$(selector).after(function(index))

Parameter Description

function(index)

Required. Specifies a function that returns the content to be inserted.

index - Optional. Receives the index position of the selector.

Method 1:
Insert content after each p element:

The code is as follows:

$("button").click(function(){
      $("p").after("<p>Hello world!</p>");
});

Method 2:

The code is as follows:

$("button").click(function(){
    $("p").after(function(n){
            
return
 "<p>The p element above has index " + n + "</p>";
    });
});

after in function must return an html string .

The above is the detailed content of Analyze the two uses of after in jQuery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn